//https://oj.uz/problem/view/CEOI16_match
#include<bits/stdc++.h>
const int N = 1e5 + 5;
using namespace std;
string s, x;
int n, nxt[N][26], f[N];
void solve(int l, int r){
if (l > r) return;
x[l] = '(';
int mat = nxt[r][s[l] - 'a'];
x[mat] = ')';
solve(l+1, mat-1); solve(mat+1, r);
}
int main(){
cin >> s;
n = s.size();
for (int i = 0; i < n; i++){
if (i > 0 && s[i-1] == s[i]) f[i] = i-1;
else {
if (i == 0) f[i] = -1;
else f[i] = nxt[i-1][s[i] - 'a'];
}
for (int j = 0; j < 26; j++){
if (f[i] > 0) {
if (s[i]-'a' != j) nxt[i][j] = nxt[f[i]-1][j];
else nxt[i][j] = i;
}
else nxt[i][j] = -1;
}
}
stack <int> ms;
for (int i = 0; i < n; i++){
if (ms.size() && ms.top() == s[i] - 'a') ms.pop();
else ms.push(s[i] - 'a');
}
if (ms.size()) return cout << -1, 0;
x = s;
solve(0, n-1);
cout << x;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Execution timed out |
2051 ms |
384 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Execution timed out |
2051 ms |
384 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |