#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF LLONG_MAX
#define MOD 1000000007
#define MAXN 100000
using namespace std;
vvi dp(MAXN+1,vi(26));
string s;
char ans[MAXN+1];
void solve(int l,int r){
if (l>r)return;
ans[l]='(';
int ind=dp[r][s[l]-'a'];
ans[ind]=')';
solve(l+1,ind-1);
solve(ind+1,r);
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>s;
int n=s.size();
reverse(s.begin(),s.end());
s+=" ";
reverse(s.begin(),s.end());
stack <int> st;
for (int i=1;i<=n;i++){
if (!st.empty() && st.top()==s[i]-'a')st.pop();
else st.push(s[i]-'a');
}
if (!st.empty())cout<<-1,exit(0);
for (int i=1;i<=n;i++){
dp[i][s[i]-'a']=i;
if (i==1 || dp[i-1][s[i]-'a']==0)continue;
for (int j=0;j<26;j++)if (j!=s[i]-'a')dp[i][j]=dp[dp[i-1][s[i]-'a']-1][j];
}
solve(1,n);
for (int i=1;i<=n;i++)cout<<ans[i];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24668 KB |
Output is correct |
2 |
Correct |
13 ms |
24596 KB |
Output is correct |
3 |
Correct |
17 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24668 KB |
Output is correct |
2 |
Correct |
13 ms |
24596 KB |
Output is correct |
3 |
Correct |
17 ms |
24668 KB |
Output is correct |
4 |
Correct |
14 ms |
24668 KB |
Output is correct |
5 |
Correct |
13 ms |
24668 KB |
Output is correct |
6 |
Correct |
13 ms |
24568 KB |
Output is correct |
7 |
Correct |
13 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24668 KB |
Output is correct |
2 |
Correct |
13 ms |
24596 KB |
Output is correct |
3 |
Correct |
17 ms |
24668 KB |
Output is correct |
4 |
Correct |
14 ms |
24668 KB |
Output is correct |
5 |
Correct |
13 ms |
24668 KB |
Output is correct |
6 |
Correct |
13 ms |
24568 KB |
Output is correct |
7 |
Correct |
13 ms |
24668 KB |
Output is correct |
8 |
Correct |
14 ms |
24744 KB |
Output is correct |
9 |
Correct |
13 ms |
24664 KB |
Output is correct |
10 |
Correct |
14 ms |
24668 KB |
Output is correct |
11 |
Correct |
16 ms |
24912 KB |
Output is correct |
12 |
Correct |
18 ms |
25948 KB |
Output is correct |
13 |
Correct |
20 ms |
26108 KB |
Output is correct |
14 |
Correct |
20 ms |
26324 KB |
Output is correct |
15 |
Correct |
21 ms |
26972 KB |
Output is correct |
16 |
Correct |
20 ms |
26972 KB |
Output is correct |
17 |
Correct |
23 ms |
26976 KB |
Output is correct |
18 |
Correct |
20 ms |
25948 KB |
Output is correct |
19 |
Correct |
22 ms |
26204 KB |
Output is correct |
20 |
Correct |
18 ms |
26204 KB |
Output is correct |
21 |
Correct |
26 ms |
26636 KB |
Output is correct |