#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=1e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
string a;
int n;
int last[mxn+10][27];
string ans;
bool solve(int l,int r){
if(r<l)return true;
if(l==-1||r==-1||last[r][a[l]-'a']==-1)return false;
ans[l]='(';
ans[last[r][a[l]-'a']]=')';
if(!solve(l+1,last[r][a[l]-'a']-1))return false;
if(!solve(last[r][a[l]-'a']+1,r))return false;
return true;
}
int32_t main(){
fastio
cin>>a;
n=a.size();
for(int i=0;i<n;i++){
for(int j=0;j<=26;j++)last[i][j]=-1;
for(int j=0;j<=26;j++)if(i&&(last[i-1][a[i]-'a']>0))last[i][j]=last[last[i-1][a[i]-'a']-1][j];
last[i][a[i]-'a']=i;
}
ans.resize(n);
if(!solve(0,n-1))cout<<-1;
else 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 |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2652 KB |
Output is correct |
12 |
Correct |
6 ms |
15872 KB |
Output is correct |
13 |
Correct |
6 ms |
15964 KB |
Output is correct |
14 |
Correct |
9 ms |
18252 KB |
Output is correct |
15 |
Correct |
8 ms |
20824 KB |
Output is correct |
16 |
Correct |
8 ms |
20828 KB |
Output is correct |
17 |
Correct |
11 ms |
20828 KB |
Output is correct |
18 |
Correct |
9 ms |
21848 KB |
Output is correct |
19 |
Correct |
10 ms |
22364 KB |
Output is correct |
20 |
Correct |
6 ms |
15964 KB |
Output is correct |
21 |
Correct |
13 ms |
22892 KB |
Output is correct |