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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int nax=1e6+5;
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<char> ans(nax);
string t;
int dp[nax][26];
void rec(int l,int r){
if(l>r) return;
ans[l]='(';
ans[dp[r][t[l]-'a']]=')';
rec(l+1,dp[r][t[l]-'a']-1);
rec(dp[r][t[l]-'a']+1,r);
}
int main()
{
optimise;
cin>>t;
int n=t.size();
reverse(t.begin(),t.end());
t.pb(' ');
reverse(t.begin(),t.end());
stack<char> st;
for (int i = 1; i <= n; ++i)
{
if(!st.empty()&&st.top()==t[i]) st.pop();
else st.push(t[i]);
}
if(!st.empty()){
cout <<-1<<endl;
return 0;
}
for (int i = 1; i <= n; ++i)
{
dp[i][t[i]-'a']=i;
if(i==1||dp[i-1][t[i]-'a']==0) continue;
for (int j = 0; j < 26; ++j)
{
if(j!=(t[i]-'a')) dp[i][j]=dp[dp[i-1][t[i]-'a']-1][j];
}
}
rec(1,n);
for (int i = 1; i <= n; ++i)
{
cout <<ans[i];
}cout <<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |