제출 #1179342

#제출 시각아이디문제언어결과실행 시간메모리
1179342ByeWorldMatch (CEOI16_match)C++20
100 / 100
35 ms18676 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
// #define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
const int MAXN = 1e5+10;
const int MAXA = 1e9;
const int INF = 2e9+100;
const int SQRT = 500;
const int LOG = 19;
const int MOD = 998244353;
void chmn(auto &a, auto b){ a = min(a, b); }
void chmx(auto &a, auto b){ a = max(a, b); }
int sum(int a, int b){ a %= MOD; b %= MOD; return (a+b)%MOD; }
void chsum(int &a, int b){ a %= MOD; b %= MOD; a = (a+b)%MOD; }
void chsub(int &a, int b){ a %= MOD; b %= MOD; a = (a+MOD-b)%MOD; }
int mul(int a, int b){ a %= MOD; b %= MOD; return a*b%MOD;}
void chmul(int &a, int b){ a = a*b%MOD; }
int expo(int a, int b){
	if(b==0) return 1;
	int te = expo(a, b/2); te = mul(te, te);
	return (b%2 ? mul(te, a) : te);
}

string s, ans;
vector<int> op;
int n;

bool DEC(int id){
	vector<int>stac = op;
	stac.pb(id); // buka
	for(int i=id+1; i<=n; i++){
		if(stac.empty()){
			stac.pb(i); continue;
		}
		if(s[stac.back()] == s[i]) stac.pop_back();
		else stac.pb(i);
	}
	return (stac.empty());
}

int dp[MAXN][28];
string solve(int l, int r){
	if(l>r) return "";
	return "(" + solve(l+1, dp[r][s[l]-'a']-1) + ")" + 
		solve(dp[r][s[l]-'a']+1, r);
}
signed main(){
	// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>s;
	n = s.size();
	s = '.'+s;
	if(!DEC(1)){
		cout << "-1\n"; exit(0);
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<26; j++){
			if(s[i] == 'a'+j) dp[i][j] = i;
			else {
				if(dp[i-1][s[i]-'a']<=0) continue;
				dp[i][j] = dp[ dp[i-1][s[i]-'a']-1 ][j];
			}
			// cout << i << ' ' << j << " ij\n";
		}
	}
	// cout << " x\n";
	cout << solve(1,n) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...