Skip to content

Commit ddfd74e

Browse files
Create 19-Hiccup.cpp
1 parent ac4ba81 commit ddfd74e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

FunctionCup/19-Hiccup.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "hiccup.h"
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int a[1000000], lb=0, rb=1000000, mb;
6+
7+
bool chk(string &s, int l, int r, int b=0) {
8+
if(l>r)
9+
return 1;
10+
for(; s[r]=='!'; --r, ++b);
11+
return b<mb?0:chk(s, a[r]+1, r-1)&&chk(s, l, a[r]-1, b-mb);
12+
}
13+
14+
int HicCup(string s) {
15+
int n=s.size();
16+
if(s[0]=='!')
17+
return -1;
18+
for(int i=0; i+1<n; ++i)
19+
if(s[i]=='H'&&s[i+1]=='!')
20+
return -1;
21+
vector<int> t;
22+
for(int i=0; i<n; ++i) {
23+
if(s[i]=='C') {
24+
if(t.empty())
25+
return -1;
26+
a[i]=t.back();
27+
t.pop_back();
28+
} else if(s[i]=='H')
29+
t.push_back(i);
30+
}
31+
if(t.size())
32+
return -1;
33+
while(lb<rb) {
34+
mb=(lb+rb+1)/2;
35+
if(chk(s, 0, n-1))
36+
lb=mb;
37+
else
38+
rb=mb-1;
39+
}
40+
return lb;
41+
}

0 commit comments

Comments
 (0)