#include "hiccup.h"
#include <utility>
#include <stack>
using namespace std;
typedef pair<char, int> pci;
bool check(const string& S, int x){
stack<char> T;
int n = S.size();
for(int i=0; i<n; i++){
if(S[i]=='H') T.push('(');
else{
if(T.empty()) return false;
if(T.top()=='(') T.pop();
}
}
return T.empty();
/*
stack<pci> T;
bool series = false;
for(int i=0, st=0; i<n; i++){
if(S[i]=='H'){
st = 1;
}
if(S[i]=='C'){
st = 2;
}
if(S[i]=='!'){
if(st>0 && st!=2) return false;
st = (st>0 ? -1 : st-1);
}
T.push({S[i], st});
if(st<=-x){
for(int j=0; j<x+2; j++) T.pop();
st = (T.empty() ? 0 : T.top().second);
nonempty = true;
}
}
*/
return false;
}
int HicCup(std::string S) {
int n = S.size();
int s = 0, e = 1000000;
return check(S, 0) ? 0 : -1;
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;
}
Compilation message
hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:49:6: warning: unused variable 'n' [-Wunused-variable]
int n = S.size();
^
# |
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 |
21 ms |
3200 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
16 ms |
3328 KB |
Output is correct |
8 |
Correct |
23 ms |
3328 KB |
Output is correct |
9 |
Correct |
21 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 |
21 ms |
3200 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
16 ms |
3328 KB |
Output is correct |
8 |
Correct |
23 ms |
3328 KB |
Output is correct |
9 |
Correct |
21 ms |
3328 KB |
Output is correct |
10 |
Correct |
13 ms |
3328 KB |
Output is correct |
11 |
Correct |
14 ms |
3200 KB |
Output is correct |
12 |
Correct |
13 ms |
3328 KB |
Output is correct |
13 |
Correct |
12 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
13 ms |
3316 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Incorrect |
13 ms |
3328 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |