#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n;
int dp[N][30];
string t,ans;
stack<char> s;
void solve(int l,int r){
if(l>r)return;
int m=dp[r][t[l-1]-'a'];
ans[m-1]=')';
solve(l+1,m-1);
solve(m+1,r);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> t;
int n=t.size();
for(auto x:t){
if(!s.empty()&&x==s.top())s.pop();
else s.emplace(x);
}
if(!s.empty())cout << -1,exit(0);
ans.assign(n,'(');
for(int i=1;i<=n;i++){
int p=dp[i-1][t[i-1]-'a'];
for(int j=0;j<26;j++)if(p>1){
dp[i][j]=t[p-2]=='a'+j?p-1:dp[p-1][j];
}
dp[i][t[i-1]-'a']=i;
}
solve(1,n);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
6 ms |
9948 KB |
Output is correct |
13 |
Correct |
4 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
10332 KB |
Output is correct |
15 |
Correct |
6 ms |
13148 KB |
Output is correct |
16 |
Correct |
6 ms |
13148 KB |
Output is correct |
17 |
Correct |
8 ms |
13148 KB |
Output is correct |
18 |
Correct |
7 ms |
12184 KB |
Output is correct |
19 |
Correct |
7 ms |
13148 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
8 ms |
14172 KB |
Output is correct |