#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 = 100000;
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][m];
//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 |
76 ms |
77852 KB |
Output is correct |
3 |
Correct |
74 ms |
77816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
77816 KB |
Output is correct |
2 |
Correct |
76 ms |
77852 KB |
Output is correct |
3 |
Correct |
74 ms |
77816 KB |
Output is correct |
4 |
Incorrect |
81 ms |
77900 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
77816 KB |
Output is correct |
2 |
Correct |
76 ms |
77852 KB |
Output is correct |
3 |
Correct |
74 ms |
77816 KB |
Output is correct |
4 |
Incorrect |
81 ms |
77900 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |