이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#pragma GCC optimize ("03,unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
int n;
string str;
string res;
bool is_possible(){
vector<char>p;
for(int j=0;j<n;j++){
if(p.empty()||p.back()!=str[j]){
p.push_back(str[j]);
}else{
p.pop_back();
}
}
return p.empty();
}
int dp[100000][26];
void solve(int l,int r){
if(l>=r)
return;
int i=dp[r-1][str[l]-'a'];
res[i]=')';
res[l]='(';
solve(l+1,i);
solve(i+1,r);
}
void solve(){
cin>>str;
n=(int)str.size();
if(!is_possible()){
cout<<"-1\n";
return;
}
for(int i=0;i<n;i++){
for(int j=0;j<26;j++)
dp[i][j]=-1;
int ch=str[i]-'a';
int pr=-1;
if(i)
pr=dp[i-1][ch];
for(int j=0;j<26;j++){
if(pr>0){
if(str[pr-1]==j+'a')
dp[i][j]=pr-1;
else
dp[i][j]=dp[pr-1][j];
}
}
dp[i][ch]=i;
}
res=string(n,'-');
solve(0,n);
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("input.in","r",stdin);freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
abbaaa
(()())
abab
-1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |