# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
51389 | spencercompton | Match (CEOI16_match) | C++17 | 2 ms | 492 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>
using namespace std;
int main(){
string str;
cin >> str;
int n = (int)str.length();
vector<int> li[26];
for(int i = 0; i<n; i++){
li[str[i]-'a'].push_back(i);
}
bool ok = true;
for(int i = 0; i<26; i++){
ok &= li[i].size()%2==0;
}
if(!ok){
cout << -1 << endl;
return 0;
}
int bef[n];
bool left[n];
for(int i = 0; i<n; i++){
bef[i] = -1;
left[i] = false;
}
for(int i =0; i<26; i++){
for(int j = 0; j<li[i].size()/2; j++){
int ri = li[i].size()-1-j;
bef[li[i][ri]] = li[i][j];
left[li[i][j]] = true;
left[li[i][ri]] = false;
}
}
int ch = 0;
int last[n+2];
for(int i = 0; i<=n+1; i++){
last[i] = -1;
}
for(int i =0 ; i<n; i++){
if(left[i]){
last[ch++] = i;
}
else{
ch--;
if(ch<0){
ok = false;
break;
}
ok &= bef[i]==last[ch];
last[ch] =-1;
}
}
if(!ok){
cout << -1 << endl;
}
else{
string t = "";
for(int i= 0 ; i<n; i++){
if(left[i]){
t += "(";
}
else{
t += ")";
}
}
cout << t << endl;
}
}
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... |