fork download
  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<memory.h>
  4. #include<limits.h>
  5. #include<string.h>
  6. using namespace std;
  7.  
  8. char str[100005];
  9. int hashmap[300];
  10. int main()
  11. {
  12. int t,n,i,j,maxlen,prev_index,cur_len;
  13. scanf("%d",&t);
  14. while(t--)
  15. {
  16. memset(hashmap,-1,sizeof(hashmap));
  17. scanf("%s",str);
  18. maxlen=1;
  19. cur_len=1;
  20. hashmap[str[0]]=0;
  21. n=strlen(str);
  22. for(i=1;i<n;i++)
  23. {
  24. prev_index=hashmap[str[i]];
  25.  
  26. if(prev_index==-1||i-cur_len>prev_index)
  27. {
  28. cur_len++;
  29. }
  30. else
  31. {
  32. if(maxlen<cur_len)
  33. maxlen=cur_len;
  34.  
  35. cur_len=i-prev_index;
  36. }
  37. hashmap[str[i]]=i;
  38. }
  39. if(cur_len>maxlen)
  40. maxlen=cur_len;
  41.  
  42. printf("%d\n",maxlen);
  43. }
  44. // your code goes here
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5284KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
2
7
7
7
7
7
7
7
7
7