답안 #610422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
610422 2022-07-28T07:40:22 Z CSQ31 괄호 문자열 (CEOI16_match) C++17
100 / 100
21 ms 11200 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+1;
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
void bad(){
	cout<<-1;
	exit(0);
}
int dp[26][MAXN];
char ans[MAXN];
string s;
void solve(int l,int r){ //assume [l,r] is solvable
	if(l>r)return;
	if(s[l] == s[r]){ //this is always solvable
		ans[l] = '(';
		ans[r] = ')';
		solve(l+1,r-1);
		return;
	}
	int mid = dp[s[l]-'a'][r];
	ans[l] = '(';
	ans[mid] = ')';
	solve(l+1,mid-1);
	solve(mid+1,r);
}
int main()
{
	cin>>s;
	int n = s.length();
	stack<int>stk;
	for(int i=0;i<n;i++){
		if(stk.empty() || s[stk.top()] != s[i])stk.push(i);
		else stk.pop();
	}
	if(!stk.empty())bad();
	for(int i=0;i<n;i++){
		for(int j=0;j<26;j++)dp[j][i] = -1;
	}
	for(int i=0;i<n;i++){
		dp[s[i]-'a'][i] = i; //empty string
		if(!i)continue;
		int cur = dp[s[i]-'a'][i-1];
		//kek(.....b),a(......)a
		cur--;
		if(cur<0)continue;
		for(int j=0;j<26;j++)dp[j][i] = max(dp[j][i],dp[j][cur]);
	}
	solve(0,n-1);
	for(int i=0;i<n;i++)cout<<ans[i];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 9 ms 6740 KB Output is correct
13 Correct 15 ms 7308 KB Output is correct
14 Correct 11 ms 8020 KB Output is correct
15 Correct 13 ms 8892 KB Output is correct
16 Correct 12 ms 8916 KB Output is correct
17 Correct 13 ms 9428 KB Output is correct
18 Correct 14 ms 9916 KB Output is correct
19 Correct 16 ms 10704 KB Output is correct
20 Correct 11 ms 6852 KB Output is correct
21 Correct 21 ms 11200 KB Output is correct