답안 #30714

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
30714 2017-07-26T08:12:14 Z zscoder 괄호 문자열 (CEOI16_match) C++14
10 / 100
0 ms 2628 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
 
bool err;
string s;

void solve(int l, int r)
{
	if(l>r) return ;
	stack<char> S;
	string ans;
	int idx=-1;
	if(s[l]==s[r])
	{
		solve(l+1,r-1);
		if(err) return ;
		s[l]='('; s[r]=')';
		return ;
	}
	for(int i = r; i > l; i--)
	{
		if(!S.empty()&&S.top()==s[i])
		{
			S.pop();
		}
		else
		{
			S.push(s[i]);
		}
		if(s[l]==s[i-1]&&S.empty())
		{
			idx=i;
			break;
		}
	}
	if(idx<=l)
	{
		err=true;
		return ;
	}
	/*
	string l = s.substr(1,idx-2);
	string r = s.substr(idx,int(s.length()) - idx);
	l = solve(l);
	if(err) return "";
	r = solve(r);
	if(err) return "";
	return "("+l+")"+r;
	*/
	solve(l+1,idx-2);
	solve(idx,r);
	s[l]='('; s[idx-1]=')';
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	//for(int i = 0; i < 100000; i++) s+='a';
	cin>>s;
	/*
	for(int i=0;i<n;i++)
	{
		stack<char> S;
		for(int j=i;j<n;j++)
		{
			if(!S.empty()&&S.top()==s[j])
			{
				S.pop();
			}
			else S.push(s[j]);
			if(S.empty()) 
			{
				//cerr<<i<<' '<<j<<'\n';
				dp[i][j]=1;
			}
		}
	}
	*/
	solve(0,int(s.length())-1);
	if(err) cout<<-1<<'\n';
	else cout<<s<<'\n';	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2180 KB Output is correct
2 Correct 0 ms 2180 KB Output is correct
3 Correct 0 ms 2180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2180 KB Output is correct
2 Correct 0 ms 2180 KB Output is correct
3 Correct 0 ms 2180 KB Output is correct
4 Correct 0 ms 2312 KB Output is correct
5 Correct 0 ms 2180 KB Output is correct
6 Runtime error 0 ms 2628 KB Execution killed because of forbidden syscall writev (20)
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2180 KB Output is correct
2 Correct 0 ms 2180 KB Output is correct
3 Correct 0 ms 2180 KB Output is correct
4 Correct 0 ms 2312 KB Output is correct
5 Correct 0 ms 2180 KB Output is correct
6 Runtime error 0 ms 2628 KB Execution killed because of forbidden syscall writev (20)
7 Halted 0 ms 0 KB -