#include "hiccup.h"
#include <utility>
#include <stack>
#include <cstdlib>
using namespace std;
struct state {
int a, c, g; // {^, H, C}, !count, ghost
};
typedef pair<char, state> pcs;
int n;
bool check(const string& S, int x){
stack<pcs> T;
state now={0,0,0};
T.push({'^', now});
for(int i=0; i<n; i++){
if(S[i]=='H'){
/* if(now.a==0){
if(now.g) while(T.top().first!='^') T.pop();
if(T.top().first!='^') return false;
}
else if(now.a==1){
if(now.g) while(T.top().first!='H') T.pop();
if(T.top().first!='H') return false;
}*/
// now = T.empty() ? {0,0,0} : T.top().second;
now.a = 1, now.c = 0, now.g = 0;
}
if(S[i]=='C'){
if(now.a != 1) return false;
now.a = 2, now.c = 0, now.g = 0;
}
if(S[i]=='!'){
if(now.a != 2){
if(!now.g) return false;
continue;
}
now.c++;
}
T.push({S[i], now});
if(now.c>x) exit(42);
if(now.c>=x && now.a==2){
for(int j=0; j<x+2; j++) T.pop();
now = T.top().second;
now.g = 1;
}
}
return now.a==0;
}
int HicCup(std::string S) {
n = S.size();
int s = 0, e = 1000000;
while(s<e){
int mid = (s+e+1)/2;
if(check(S, mid)) s = mid;
else e = mid-1;
}
return check(S, s) ? s : -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
28 ms |
3328 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
14 ms |
3328 KB |
Output is correct |
8 |
Correct |
32 ms |
3200 KB |
Output is correct |
9 |
Correct |
30 ms |
3328 KB |
Output is correct |
10 |
Correct |
13 ms |
3328 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
28 ms |
3328 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
14 ms |
3328 KB |
Output is correct |
8 |
Correct |
32 ms |
3200 KB |
Output is correct |
9 |
Correct |
30 ms |
3328 KB |
Output is correct |
10 |
Correct |
13 ms |
3328 KB |
Output is correct |
11 |
Correct |
15 ms |
3328 KB |
Output is correct |
12 |
Correct |
19 ms |
3352 KB |
Output is correct |
13 |
Correct |
29 ms |
4740 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
17 ms |
3392 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
76 ms |
3328 KB |
Output is correct |
20 |
Correct |
27 ms |
3328 KB |
Output is correct |
21 |
Correct |
195 ms |
13352 KB |
Output is correct |
22 |
Correct |
200 ms |
16156 KB |
Output is correct |
23 |
Correct |
435 ms |
19116 KB |
Output is correct |
24 |
Correct |
21 ms |
3456 KB |
Output is correct |
25 |
Correct |
27 ms |
3328 KB |
Output is correct |
26 |
Correct |
28 ms |
3456 KB |
Output is correct |
27 |
Correct |
207 ms |
13776 KB |
Output is correct |
28 |
Correct |
326 ms |
19240 KB |
Output is correct |
29 |
Correct |
330 ms |
19768 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
128 KB |
Output is correct |
32 |
Correct |
26 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |