#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=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
string a;
bool can[2002][2002];
vector<int>pos[50];
int n;
bool check(string cur){
stack<int>st;
for(int i=0;i<n;i++){
if(cur[i]==')'){
if(st.empty()||a[st.top()]!=a[i]||cur[st.top()]==')')return false;
st.pop();
}
if(cur[i]=='(')st.push(i);
if(cur[i]=='?'){
if(!st.empty()&&a[st.top()]==a[i]&&cur[st.top()]!=')')st.pop();
else st.push(i);
}
}
return st.empty();
}
int32_t main(){
cin>>a;
n=a.size();/*
for(int i=0;i<n;i++)pos[a[i]-'a'].pb(i);
for(int i=0;i<=26;i++)reverse(all(pos[i]));
for(int i=0;i<n;i++){
stack<int>st;
for(int j=i;j<n;j++){
if(!st.empty()&&a[st.top()]==a[j])st.pop();
else st.push(j);
can[i][j]=st.empty();
}
}*/
string ans;
ans.resize(n,'?');
for(int i=0;i<n;i++){
ans[i]='(';
if(!check(ans))ans[i]=')';
}
/*
for(int i=0;i<n;i++){
if(ans[i]!='?')continue;
int p=-1;
for(auto j:pos[a[i]-'a']){
if(ans[j]!='?')continue;
if(i!=j-1&&can[i+1][j-1]==false)continue;
p=j;
break;
}
if(p==-1||i==p)return cout<<-1,0;
ans[i]='(';
ans[p]=')';
}
stack<int>st;
for(int i=0;i<n;i++){
if(ans[i]=='?')return cout<<-1,0;
if(ans[i]==')'&&(st.empty()||st.top()!=0))return cout<<-1,0;
else if(ans[i]=='(')st.push(0);
}*/
if(check(ans))cout<<ans;
else cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
12 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
12 ms |
348 KB |
Output is correct |
8 |
Correct |
159 ms |
348 KB |
Output is correct |
9 |
Correct |
176 ms |
456 KB |
Output is correct |
10 |
Correct |
162 ms |
892 KB |
Output is correct |
11 |
Correct |
144 ms |
472 KB |
Output is correct |
12 |
Execution timed out |
2029 ms |
880 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |