fork(8) download
  1. #include <cstdio>
  2. #include <stack>
  3.  
  4. char S[1000001];
  5.  
  6. struct record_t {
  7. int top, bottom, pos;
  8. };
  9.  
  10. int main() {
  11. std::stack<record_t> stack;
  12. scanf("%*d %s", S);
  13. int level = 0, pos = 1, answer = 0;
  14. record_t record = {0,0,0};
  15. stack.push(record);
  16. for( char *s = S; *s; s++, pos++ ) {
  17. if( *s == 'p' ) level++;
  18. else level--;
  19.  
  20. record_t record = {level,level,pos};
  21.  
  22. while( not stack.empty() and level >= stack.top().top ) {
  23. record.pos = stack.top().pos;
  24. record.bottom = stack.top().bottom;
  25. stack.pop();
  26. }
  27.  
  28. while( not stack.empty() and level < stack.top().bottom ) {
  29. stack.pop();
  30. }
  31.  
  32. if( pos-record.pos > answer )
  33. answer = pos-record.pos;
  34.  
  35. stack.push(record);
  36. }
  37. printf("%d\n", answer );
  38. }
Success #stdin #stdout 0s 4456KB
stdin
15
pppjjpjpjpjjppj
stdout
5