#include <bits/stdc++.h>
using namespace std;
#define ll long long
mt19937_64 rnd(time(0));
vector<vector<ll>> ve;
vector<ll> ha, pr;
vector<char> ans;
void rec(int l, int r, int ind){
if (l > r){
return;
}
ll nl = l, nr = r;
while ((pr[ve[ind][nr]] ^ pr[ve[ind][nl] - 1]) != 0){
nr -= 2;
}
ans[ve[ind][nr]] = ')';
ans[ve[ind][nl]] = '(';
rec(nr + 1, r, ind);
rec(nl + 1, nr - 1, ind);
}
int main(){
string s;
cin>>s;
int n = s.size();
s = " " + s;
ve.resize(26), ha.assign(26, 0), pr.assign(n + 1, 0), ans.assign(n + 1, -1);
for (int i = 0; i < 26; i++){
ha[i] = rnd();
}
for (int i = 1; i <= n; i++){
pr[i] = pr[i - 1] ^ ha[s[i] - 'a'];
ve[s[i] - 'a'].push_back(i);
}
deque<int> de;
for (int i = 1; i <= n; i++){
int ch = s[i] - 'a';
if (de.size() == 0 || de.back() != ch){
de.push_back(ch);
}
else{
de.pop_back();
}
}
if (de.size() != 0){
cout<<-1;
}
else{
for (int i = 0; i < 26; i++){
rec(0, ve[i].size() - 1, i);
}
for (int i = 1; i <= n; i++){
cout<<ans[i];
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |