#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const int mod1 = 1e9 + 7;
vector<pair<int,int> > mp[26];
bool res[maxn];
string s;
stack<int> stk,hash1,hash2;
void rec(int l,int r){
if(l > r){
return;
}
bool check = false;
if(!stk.empty() && stk.top() == s[l]-'a'){
stk.pop();
hash1.pop();
check = true;
}else{
int nhash1 = (27ll*hash1.top()+s[l]-'a'+1) % mod1;
stk.push(s[l]-'a');
hash1.push(nhash1);
}
int nxt = (*prev(upper_bound(mp[s[l]-'a'].begin(),mp[s[l]-'a'].end(),make_pair(hash1.top(),r)))).second;
res[nxt] = true;
rec(l+1,nxt-1);
if(check){
int nhash1 = (27ll*hash1.top()+s[l]-'a'+1) % mod1;
stk.push(s[l]-'a');
hash1.push(nhash1);
}else{
stk.pop();
hash1.pop();
}
rec(nxt+1,r);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
int n = s.size();
hash1.push(0);
hash2.push(0);
for(int i=n-1;i>=0;i--){
if(!stk.empty() && stk.top() == s[i]-'a'){
stk.pop();
hash1.pop();
}else{
int nhash1 = (27ll*hash1.top()+s[i]-'a'+1) % mod1;
stk.push(s[i]-'a');
hash1.push(nhash1);
}
mp[s[i]-'a'].push_back(make_pair(hash1.top(),i));
}
for(int i=0;i<26;i++){
sort(mp[i].begin(),mp[i].end());
}
if(!stk.empty()){
cout << "-1\n";
return 0;
}
rec(0,n-1);
for(int i=0;i<n;i++){
cout << (res[i] ? ')' : '(');
}
cout << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
556 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
11 ms |
2644 KB |
Output is correct |
13 |
Correct |
15 ms |
2820 KB |
Output is correct |
14 |
Correct |
15 ms |
3228 KB |
Output is correct |
15 |
Correct |
18 ms |
3952 KB |
Output is correct |
16 |
Correct |
16 ms |
3888 KB |
Output is correct |
17 |
Correct |
18 ms |
4000 KB |
Output is correct |
18 |
Correct |
19 ms |
2596 KB |
Output is correct |
19 |
Correct |
18 ms |
3536 KB |
Output is correct |
20 |
Correct |
10 ms |
3028 KB |
Output is correct |
21 |
Correct |
20 ms |
3896 KB |
Output is correct |