# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
127287 | sealnot123 | 괄호 문자열 (CEOI16_match) | C++14 | 2 ms | 376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define x first
#define y second
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define push_back pb
#define emplace_back eb
using namespace std;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 100007;
char str[N], ans[N];
int cnt[26], onstack[26];
stack<int> sta;
void PUSH(int a, int b){
onstack[b]++;
sta.push(a);
}
bool del(){
if(SZ(sta)==1) return false;
int a = sta.top(), b = str[a]-'a';
sta.pop();
if(b == str[sta.top()]-'a'){
sta.pop(), onstack[b] -= 2, cnt[b] -= 2;
ans[a] = ')';
return true;
}
bool res = del();
sta.push(a);
return res;
}
int main(){
int i,j,k,l,a,b,c,d;
scanf("%s",str+1);
int n = strlen(str+1);
for(i=1;i<=n;i++) cnt[str[i]-'a']++;
for(i=0;i<26;i++){
if(cnt[i]&1){
printf("-1");
return 0;
}
}
for(i=1;i<=n;i++){
a = str[i]-'a';
ans[i] = '(';
PUSH(i, a);
while(2*onstack[a] > cnt[a]){
if(!del()){
printf("-1");
return 0;
}
}
/*printf("==%d %s\n",i,ans+1);*/
}
d = 0;
for(i=1;i<=n;i++){
d += (ans[i]=='(')?1:-1;
if(d<0) assert(0);
}
assert(d==0);
printf("%s",ans+1);
return 0;
}
/*
abbccddabbccdd
abbacabbac
accaccacca
aaaabbbb
aaabcaacba (()((())))
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |