#include<bits/stdc++.h>
using namespace std;
#define MAXN 200000
string s;
int n, op[MAXN][26];
char ans[MAXN];
string solve(int l, int r) {
if (l > r) return "";
return '(' + solve(l + 1, op[r][s[l] - 97] - 1) + ')' + solve(op[r][s[l] - 97] + 1, r);
}
int main () {/*
freopen("match.in", "r", stdin);
freopen("match.out", "w", stdout);*/
cin >> s;
n=s.size();
vector<char> st;
for(int i = 0;i<n;i++) {
if(st.size() and s[i]==st[st.size()-1])st.pop_back();
else st.push_back(s[i]);
for(int j = 0;j<26;j++) {
if(j == s[i]-97)op[i][j] = i;
else if(i > 0 && op[i][s[i]-97])op[i][j] = op[op[i-1][s[i]-97]-1][j];
else op[i][j] = -1;
}
}
if(st.size())cout<< -1;
else {
cout<<solve(0, n-1)<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Runtime error |
226 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Runtime error |
226 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |