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;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ll long long
#define int ll
#define pii pair<int, int>
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define M_PI 3.14159265358979323846
#define all(v) v.begin(), v.end()
#define pss pair<string, string>
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
#define imp cout<<-1<<endl;
#define flu cout.flush();
#define Endl endl
const int N = 100009;
const int mod = 1e9+7;
map<int, int>lm, rm;
void solve(){
string a;
cin>>a;
int n=a.size(), qwe=0, yx=0;
string test="";
stack<char>q;
for(int i=0; i<n; i++){
if(q.empty()){
test=test+"(";
q.push(a[i]);
continue;
}
char fr=q.top();
if(a[i]==fr){
test=test+")";
q.pop();
}
else{
q.push(a[i]);
test=test+"(";
}
}
for(int i=0; i<n; i++){
if(test[i]=='('){
qwe++;
}
else{
qwe--;
}
if(qwe<0){
break;
}
}
if(qwe<0 or qwe!=0){
imp;
return;
}
int l=0, r=n-1;
string left="", right="";
while(l<r){
if(lm[a[l]]!=0){
lm[a[l]]--;
left=left+")";
}
else{
lm[a[l]]++;
left=left+"(";
if(rm[a[l]]!=0){
lm[a[r]]--;
rm[a[r]]--;
}
}
if(rm[a[r]]!=0){
rm[a[r]]--;
right="("+right;
}
else{
rm[a[r]]++;
right=")"+right;
if(lm[a[r]]!=0){
lm[a[r]]--;
rm[a[r]]--;
}
}
l++;
r--;
}
cout<<left<<right<<endl;
}
signed main(){
io;
int t=1;
//cin>>t;
while(t--){
solve();
}
}
Compilation message (stderr)
match.cpp: In function 'void solve()':
match.cpp:29:28: warning: unused variable 'yx' [-Wunused-variable]
29 | int n=a.size(), qwe=0, yx=0;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |