답안 #258150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
258150 2020-08-05T12:42:59 Z youssefbou62 괄호 문자열 (CEOI16_match) C++14
100 / 100
18 ms 13824 KB
#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; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 6 ms 12032 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 6 ms 12032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 6 ms 12032 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 6 ms 12032 KB Output is correct
8 Correct 8 ms 12112 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 7 ms 12160 KB Output is correct
11 Correct 7 ms 12288 KB Output is correct
12 Correct 16 ms 13056 KB Output is correct
13 Correct 11 ms 13184 KB Output is correct
14 Correct 12 ms 13568 KB Output is correct
15 Correct 12 ms 12416 KB Output is correct
16 Correct 12 ms 12416 KB Output is correct
17 Correct 17 ms 13188 KB Output is correct
18 Correct 18 ms 12416 KB Output is correct
19 Correct 15 ms 13440 KB Output is correct
20 Correct 13 ms 13312 KB Output is correct
21 Correct 14 ms 13824 KB Output is correct