This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define pb push_back
#define iospeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int main()
{
iospeed
string s;
cin >> s;
ll n = (ll)(s.size()), i, j, k;
vector<string>vect;
for(i = 0; i < (1 << n); i++)
{
ll pref[n+5];
pref[0] = 0;
string str = "";
bool ok = true;
for(j = 0; j < n; j++)
{
pref[j+1] = pref[j];
if((i >> j) & 1){
str += ")";
pref[j+1]--;
}
else{
str += "(";
pref[j+1]++;
}
if(pref[j+1] < 0)
{
ok = false;
break;
}
}
if(!ok)
continue;
for(j = 1; j <= n; j++)
{
for(k = j + 1; k <= n; k++)
{
if(pref[k] - pref[j-1] == 0 && str[j-1] == '(' && str[k-1] == ')')
{
if(k == j + 1 || pref[k-1] - pref[j] == 0){
if(s[k-1] != s[j-1])
{
ok = false;
break;
}
}
}
}
}
if(ok && pref[n] == 0)
vect.pb(str);
}
if(vect.size() == 0)
cout << "-1\n";
else
{
sort(all(vect));
cout << vect[0] << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |