답안 #30673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
30673 2017-07-26T05:11:48 Z zscoder 괄호 문자열 (CEOI16_match) C++14
10 / 100
6 ms 6132 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 dp[2011][2011];

bool ok(int l, int r)
{
	if(l>r) return 1;
	return dp[l][r];
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	string s; cin>>s;
	int n=s.length();
	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;
			}
		}
	}
	deque<int> dq;
	dq.pb(n);
	for(int i=0;i<n;i++)
	{
		bool pos=0;
		if(s[i]>='a'&&s[i]<='z')
		{
			while(!dq.empty()&&i>dq.front()) dq.pop_front();
			for(int j=dq.front()-1;j>i;j--)
			{
				//cerr<<ok(i+1,j-1)<<' '<<ok(j+1,n-1)<<'\n';
				if(s[i]==s[j]&&ok(i+1,j-1)&&ok(j+1,dq.front()-1))
				{
					s[i]='('; s[j]=')';
					dq.push_front(j);
					pos=1;
					break;
				}
			}
			if(!pos)
			{
				cout<<-1<<'\n';
				return 0;
			}
		}
	}
	cout<<s<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6132 KB Output is correct
2 Correct 0 ms 6132 KB Output is correct
3 Correct 0 ms 6132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6132 KB Output is correct
2 Correct 0 ms 6132 KB Output is correct
3 Correct 0 ms 6132 KB Output is correct
4 Correct 0 ms 6132 KB Output is correct
5 Correct 3 ms 6132 KB Output is correct
6 Runtime error 6 ms 6132 KB Execution killed because of forbidden syscall writev (20)
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6132 KB Output is correct
2 Correct 0 ms 6132 KB Output is correct
3 Correct 0 ms 6132 KB Output is correct
4 Correct 0 ms 6132 KB Output is correct
5 Correct 3 ms 6132 KB Output is correct
6 Runtime error 6 ms 6132 KB Execution killed because of forbidden syscall writev (20)
7 Halted 0 ms 0 KB -