fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll ;
  4.  
  5. int main() {
  6.  
  7. ll t ;
  8. cin >> t ;
  9. while ( t -- )
  10. {
  11. string s ;
  12. cin >> s ;
  13. ll ind (0) , c(0) ,c1(0) , ans (s.length() ) , has [ 26 ] = { 0 } ,
  14. has2 [ 26 ] = {0};
  15. for ( ll i = 0 ; i < s.length() ; i ++ )
  16. {
  17. if ( has2 [ s [ i ] - 'a' ] == 0 )
  18. c++ ;
  19. has2 [ s [i ] -'a' ] ++ ;
  20. }
  21. for ( ll i = 0 ; i < s.length() ; i ++ )
  22. {
  23. if ( has [ s [ i ] - 'a' ] == 0 )
  24. c1 ++ ;
  25. has [ s [ i ] - 'a' ] ++ ;
  26. if( c1 >= c )
  27. {
  28. for ( ll j = ind ; j <= i ; j ++ )
  29. {
  30. if ( has [ s [ j ] - 'a' ] > 1 )
  31. {
  32. has [ s [ j ] - 'a' ] -- ;
  33. ind ++ ;
  34. }
  35. else break ;
  36. }
  37. ans = min ( ans , i - ind + 1 ) ;
  38. }
  39. }
  40. cout << ans << endl ;
  41. }
  42. // your code goes here
  43. return 0;
  44. }
Success #stdin #stdout 0s 15240KB
stdin
2
aabcbcdbca
aaab
stdout
4
2