#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 100010;
const int MAX_K = 30;
string str;
vector<char> v;
int cnt = 1, N;
vector<int> gp[MAX_N+1][MAX_K+3];
int idx[MAX_N+1][MAX_K+2];
pii arr[MAX_N+1];
vector<pii> dq;
int st[MAX_N+1];
void answer(int x, int y){
dq.push_back({x, y});
pii now;
int p, s, e, m;
while(!dq.empty()){
x = dq.back().first; y = dq.back().second;
//cout<<x<<' '<<y<<endl;
//getchar();
dq.pop_back();
if(x==y && y==-1){
printf(")"); continue;
}
if(x>=y) continue;
now = arr[x];
now.first = st[x];
//cout<<now.first<<' '<<now.second<<endl;
s = 0, e = gp[now.first][now.second].size()-1, m = 0;
while(s<e){
m = (s+e)/+1;
if(gp[now.first][now.second][m]<=y) s = m;
else e = m-1;
}
p = gp[now.first][now.second][s];
//cout<<p<<endl;
printf("(");
dq.push_back({p+1, y});
dq.push_back({-1, -1});
dq.push_back({x+1, p-1});
}
}
int main(){
cin>>str; N = str.size();
for(int i=0; i<str.size(); i++){
if(!v.empty() && v.back()==str[i]) v.pop_back();
else v.push_back(str[i]);
}
if(!v.empty()){
cout<<-1; return 0;
}
int now = 1;
for(int i=0; i<N; i++){
st[i] = now;
if(!v.empty() && v.back()==str[i]){
now = idx[now][0];
v.pop_back();
}else{
if(idx[now][str[i]-'a'+1]!=0){
now = idx[now][str[i]-'a'+1];
}else{
idx[now][str[i]-'a'+1] = ++cnt;
idx[cnt][0] = now;
now = cnt;
}
v.push_back(str[i]);
}
gp[now][str[i]-'a'+1].push_back(i);
arr[i] = {now, str[i]-'a'+1};
//cout<<arr[i].first<<" "<<arr[i].second<<endl;
}
answer(0, N-1);
return 0;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:71:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<str.size(); i++){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
77816 KB |
Output is correct |
2 |
Correct |
74 ms |
77816 KB |
Output is correct |
3 |
Correct |
72 ms |
77816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
77816 KB |
Output is correct |
2 |
Correct |
74 ms |
77816 KB |
Output is correct |
3 |
Correct |
72 ms |
77816 KB |
Output is correct |
4 |
Correct |
81 ms |
77880 KB |
Output is correct |
5 |
Correct |
78 ms |
77944 KB |
Output is correct |
6 |
Correct |
85 ms |
77944 KB |
Output is correct |
7 |
Correct |
75 ms |
77916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
77816 KB |
Output is correct |
2 |
Correct |
74 ms |
77816 KB |
Output is correct |
3 |
Correct |
72 ms |
77816 KB |
Output is correct |
4 |
Correct |
81 ms |
77880 KB |
Output is correct |
5 |
Correct |
78 ms |
77944 KB |
Output is correct |
6 |
Correct |
85 ms |
77944 KB |
Output is correct |
7 |
Correct |
75 ms |
77916 KB |
Output is correct |
8 |
Correct |
76 ms |
78312 KB |
Output is correct |
9 |
Correct |
76 ms |
78556 KB |
Output is correct |
10 |
Correct |
77 ms |
78584 KB |
Output is correct |
11 |
Correct |
77 ms |
78676 KB |
Output is correct |
12 |
Correct |
116 ms |
84500 KB |
Output is correct |
13 |
Correct |
118 ms |
85364 KB |
Output is correct |
14 |
Correct |
107 ms |
85584 KB |
Output is correct |
15 |
Correct |
92 ms |
80116 KB |
Output is correct |
16 |
Correct |
93 ms |
80116 KB |
Output is correct |
17 |
Correct |
108 ms |
83928 KB |
Output is correct |
18 |
Correct |
94 ms |
80120 KB |
Output is correct |
19 |
Correct |
125 ms |
87628 KB |
Output is correct |
20 |
Correct |
100 ms |
84724 KB |
Output is correct |
21 |
Correct |
128 ms |
87416 KB |
Output is correct |