fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. int max(int x, int y)
  4. { return (y > x)? y : x; }
  5.  
  6. int max_sum(int a[], int size)
  7. {
  8. int max_so_far = a[0], i;
  9. int curr_max = a[0];
  10.  
  11. for (i = 1; i < size; i++)
  12. {
  13. curr_max = max(a[i], curr_max+a[i]);
  14. max_so_far = max(max_so_far, curr_max);
  15. }
  16. return max_so_far;
  17. }
  18. int main()
  19. {
  20. int tc,len,i;
  21. scanf("%d",&tc);
  22. while(tc--)
  23. {
  24. char s[1000000];
  25. scanf("%s",s);
  26. len=strlen(s);
  27. int a[len],c,m;
  28. c=0;
  29. for(i=0;i<len;i++)
  30. {
  31. if(s[i]=='R')
  32. {
  33. a[i]=-1;
  34. c++;
  35. }
  36. else
  37. a[i]=1;
  38. }
  39. m=max_sum(a,len);
  40. printf("%d\n",m+c);
  41. }
  42. return 0;
  43. }
Success #stdin #stdout 0s 4300KB
stdin
2
RKKRK
RKKR
stdout
4
4