Submission #90643

# Submission time Handle Problem Language Result Execution time Memory
90643 2018-12-23T08:38:12 Z jangwonyoung Match (CEOI16_match) C++14
100 / 100
37 ms 7412 KB
#include<iostream>
#include<stack>
#include<set>
using namespace std;
string s,b;
int n;
int f[100001];
stack<char>st;
stack<int>cut;
set<pair<int,int> >g[226];
int par[100001];
int find(int x){
	if(par[x]!=x) par[x]=find(par[x]);
	return par[x];
}
void join(int u,int v){
	u=find(u);v=find(v);
	par[u]=v;
}
int main(){
	ios::sync_with_stdio(false);
	cin >> s;
	b=s;
	n=s.size();
	par[n]=n;
	for(int i=0; i<n ;i++){
		par[i]=i;
		if(st.empty() || st.top()!=s[i]) st.push(s[i]);
		else st.pop();
	}
	if(!st.empty()){
		cout << "-1\n";
		return 0;
	}
	for(int i=n-1; i>=0 ;i--){
		f[i]=-1;
		int en=i+1;
		while(en<n && en>=0){
			if(s[i]==s[en]){
				f[i]=en+1;
				break;
			}
			en=f[en];
		}
	}
	for(int i=0; i<n ;i++){
		if(f[i]!=-1) join(i,f[i]);
	}
	for(int i=0; i<n ;i++){
		g[s[i]].insert({find(i+1),i});
	}
	cut.push(n);
	for(int i=0; i<n ;i++){
		if(i==cut.top()){
			cut.pop();
			continue;
		}
		pair<int,int>tmp={find(i),cut.top()};
		auto it=g[s[i]].lower_bound(tmp);
		--it;
		b[i]='(';
		b[it->second]=')';
		cut.push(it->second);
		g[s[i]].erase(it);
	}
	cout << b << endl;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:50:9: warning: array subscript has type 'char' [-Wchar-subscripts]
   g[s[i]].insert({find(i+1),i});
         ^
match.cpp:59:17: warning: array subscript has type 'char' [-Wchar-subscripts]
   auto it=g[s[i]].lower_bound(tmp);
                 ^
match.cpp:64:9: warning: array subscript has type 'char' [-Wchar-subscripts]
   g[s[i]].erase(it);
         ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 660 KB Output is correct
5 Correct 2 ms 660 KB Output is correct
6 Correct 2 ms 660 KB Output is correct
7 Correct 2 ms 664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 660 KB Output is correct
5 Correct 2 ms 660 KB Output is correct
6 Correct 2 ms 660 KB Output is correct
7 Correct 2 ms 664 KB Output is correct
8 Correct 3 ms 964 KB Output is correct
9 Correct 3 ms 1156 KB Output is correct
10 Correct 3 ms 1156 KB Output is correct
11 Correct 3 ms 1204 KB Output is correct
12 Correct 19 ms 4208 KB Output is correct
13 Correct 18 ms 4652 KB Output is correct
14 Correct 22 ms 4972 KB Output is correct
15 Correct 26 ms 5584 KB Output is correct
16 Correct 24 ms 5660 KB Output is correct
17 Correct 23 ms 6120 KB Output is correct
18 Correct 30 ms 6460 KB Output is correct
19 Correct 37 ms 6948 KB Output is correct
20 Correct 19 ms 6948 KB Output is correct
21 Correct 37 ms 7412 KB Output is correct