fork download
  1. #include<stdio.h>
  2. int trans[256];
  3. int give[3] = { 2,0,1 };
  4. char I[121212];
  5. int S[3][121212], RS[3][121212];
  6. int mx[121212], rmx[121212];
  7. int max(int a, int b) { if (a < b)return b; return a; }
  8. int main() {
  9. trans['H'] = 0, trans['S'] = 1, trans['P'] = 2;
  10. int n;
  11. int i, j;
  12. scanf("%d", &n);
  13. for (i = 1; i <= n; i++) {
  14. scanf(" %c", &I[i]);
  15. S[0][i] = S[0][i - 1];
  16. S[1][i] = S[1][i - 1];
  17. S[2][i] = S[2][i - 1];
  18. S[give[trans[I[i]]]][i]++;
  19. mx[i] = max(max(S[0][i], S[1][i]), S[2][i]);
  20. }
  21. for (i = n; i >= 1; i--) {
  22. RS[0][i] = RS[0][i + 1];
  23. RS[1][i] = RS[1][i + 1];
  24. RS[2][i] = RS[2][i + 1];
  25. RS[give[trans[I[i]]]][i]++;
  26. rmx[i] = max(max(RS[0][i], RS[1][i]), RS[2][i]);
  27. }
  28. int ans = 0;
  29. for (i = 1; i <= n; i++) {
  30. ans = max(ans, mx[i] + rmx[i + 1]);
  31. }
  32. printf("%d", ans);
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 4192KB
stdin
Standard input is empty
stdout
22061