#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 100007;
const ll inf = 2e9;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
string s;
int n, dp[M][30];
char ans[M];
bool check(int start){
stack <int> st;
for(int i = start; i < n; ++i){
if(st.empty() || s[st.top()] != s[i]) st.push(i);
else st.pop();
}
return st.empty();
}
void solve(int l, int r){
if(l > r) return;
int mid = dp[r][s[l] - 'a'];
ans[l] = '(';
ans[mid] = ')';
solve(l + 1, mid - 1);
solve(mid + 1, r);
return;
}
int main()
{
cin >> s;
n = s.size();
if(!check(0)) return cout << -1 << endl, 0;
for(int i = 0; i < n; ++i) for(int j = 0; j < 26; ++j) dp[i][j] = -1;
dp[0][s[0] - 'a'] = 0;
for(int i = 1; i < n; ++i){
dp[i][s[i] - 'a'] = i;
for(int j = 0; j < 26; ++j) if(j != s[i] - 'a'){
int l = dp[i - 1][s[i] - 'a'];
if(l) dp[i][j] = dp[l - 1][j];
}
}
solve(0, n - 1);
for(int i = 0 ; i < n; ++i) cout << ans[i];
cout << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1332 KB |
Output is correct |
12 |
Correct |
12 ms |
8524 KB |
Output is correct |
13 |
Correct |
13 ms |
9416 KB |
Output is correct |
14 |
Correct |
18 ms |
10180 KB |
Output is correct |
15 |
Correct |
15 ms |
12108 KB |
Output is correct |
16 |
Correct |
15 ms |
12120 KB |
Output is correct |
17 |
Correct |
16 ms |
12748 KB |
Output is correct |
18 |
Correct |
17 ms |
12056 KB |
Output is correct |
19 |
Correct |
18 ms |
13040 KB |
Output is correct |
20 |
Correct |
11 ms |
8840 KB |
Output is correct |
21 |
Correct |
29 ms |
14020 KB |
Output is correct |