답안 #56828

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56828 2018-07-12T16:52:47 Z mraron 괄호 문자열 (CEOI16_match) C++14
100 / 100
88 ms 10028 KB
/*
ID: noszaly1
TASK: {TASK}
LANG: C++11               
*/

//Noszály Áron 10o Debreceni Fazekas Mihály Gimnázium

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=acos(-1);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

ll mod=1e9+7;
ll p=37;

string t;

ll st[100001], en[100001];
map<int, vector<int>> lst;
string ans;

void solve(int L, int R) {
	if(L>R) return ;
	//cerr<<L<<" "<<lst[en[L]].back()<<" "<<R<<"\n";
	while(lst[en[L]].back()>R) lst[en[L]].pop_back();
	//assert(!lst[en[L]].empty() && lst[en[L]].back()<=R);
	
	int par=lst[en[L]].back();
	
	ans[L]='(';
	ans[par]=')';
	
	lst[en[L]].pop_back();
	
	solve(par+1, R);
	solve(L+1, par-1);
}

int main() {
	IO;
	cin>>t;
	
	stack<pair<char,ll>> s;
	
	int ind=0;
	for(auto i:t) {
		if(s.empty()) {
			st[ind]=-1;
		}else {
			st[ind]=s.top().yy;
		}
		
		if(!s.empty() && s.top().xx==i) {
			s.pop();
		}else {
			if(s.empty()) {
				s.push({i, i});
			}else {
				s.push({i, (i+p*s.top().yy)%mod});
			}
		}
		
		if(s.empty()) {
			en[ind]=-1;
		}else {
			en[ind]=s.top().yy;
		}
		
		ind++;
	}
	
	if(!s.empty()) {
		cerr<<"-1\n";
		return 0;
	}

	
	for(int i=0;i<sz(t);++i) {
		//cerr<<st[i]<<" "<<en[i]<<"\n";
		lst[st[i]].pb(i);
	}
	
	ans=string(sz(t), '*');
	
	solve(0, sz(t)-1);
	
	cout<<ans<<"\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 3 ms 544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 3 ms 544 KB Output is correct
4 Correct 2 ms 544 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 4 ms 828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 3 ms 544 KB Output is correct
4 Correct 2 ms 544 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 4 ms 828 KB Output is correct
8 Correct 5 ms 844 KB Output is correct
9 Correct 7 ms 1200 KB Output is correct
10 Correct 6 ms 1380 KB Output is correct
11 Correct 7 ms 1412 KB Output is correct
12 Correct 56 ms 6404 KB Output is correct
13 Correct 60 ms 6916 KB Output is correct
14 Correct 74 ms 7956 KB Output is correct
15 Correct 19 ms 7956 KB Output is correct
16 Correct 15 ms 7956 KB Output is correct
17 Correct 60 ms 7956 KB Output is correct
18 Correct 18 ms 7956 KB Output is correct
19 Correct 76 ms 9260 KB Output is correct
20 Correct 63 ms 9260 KB Output is correct
21 Correct 88 ms 10028 KB Output is correct