Submission #258150

#TimeUsernameProblemLanguageResultExecution timeMemory
258150youssefbou62Match (CEOI16_match)C++14
100 / 100
18 ms13824 KiB
#include  <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds;
#define sz(x) (int)x.size() 
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define allarr(a) a , a + n
#define ll long long
#define ull unsigned long long 
#define pb push_back
#define fastio ios_base::sync_with_stdio(false) ; cin.tie(NULL); cout.tie(NULL)

typedef pair<int, int> pi;
typedef pair<ll,ll> pll; 
typedef pair<int,pi> trp ;
typedef vector<pi> vpi;
typedef vector<pll> vpll ;
// int ab  (int  x ) { return (x>0?x:-x); }
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int MAXN = 1e5+5 ; 
int dp[MAXN][30] ;  
string s,ans; 
int right_most_possible ( int r , char c ){

	int& ans = dp[r][(int)c-'a'] ; 
	if( ans != -1 ){
		return ans ; 
	}
	// cout << r << " " << c << " " << s[r]<< endl; 
	if( r - 2 >= 0 && s[r] == s[r-1 ] ){
		if( s[r-2] == c ){
			ans = r-2 ; 
		}else{
			ans = right_most_possible(r-2,c); 
		}
	}
	else{
		int l = right_most_possible(r-1,s[r])-1;
		if( l >= 0 ){
			if( s[l] == c )ans = l ; 
			else ans = right_most_possible(l,c); 
		}else
			cout <<"oops"<<endl; 
	}
	return ans ;
}

void solve ( int l , int r ){
	if( l > r )return ;
	//cout << l << " " << r << endl;  
	if( s[l]  == s[r] ){
		ans [l] = '(' ; 
		ans[r]= ')'; 
		solve ( l+1 , r-1 );
		return ; 
	}
	int x = right_most_possible(r,s[l]); 
	ans[l] = '(' ; 
	ans[x] = ')' ; 
	// cout << l << " " << r << " " << x << endl; 
	solve ( l + 1 , x - 1 ); 
	solve ( x + 1 , r ) ; 
}

bool test (){
	stack<char> st ; 

	for(int i = 0 ; i < sz(s) ; i++ ){
		if( !st.empty() && s[i]==st.top())
			st.pop() ; 
		else st.push(s[i]); 
	}
	return st.empty(); 
}
int main(){
	cin >> s ;
	if( !test () ){
		cout << -1 << endl; 
		return 0 ; 
	}
	ans.resize(sz(s)) ; 
	memset(dp,-1,sizeof dp); 
	solve ( 0 , sz(s)-1 ) ; 
	cout << ans << endl; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...