답안 #56825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56825 2018-07-12T16:50:50 Z mraron 괄호 문자열 (CEOI16_match) C++14
37 / 100
121 ms 17788 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=31;

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 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 4 ms 632 KB Output is correct
5 Correct 4 ms 644 KB Output is correct
6 Correct 4 ms 644 KB Output is correct
7 Correct 3 ms 696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 4 ms 632 KB Output is correct
5 Correct 4 ms 644 KB Output is correct
6 Correct 4 ms 644 KB Output is correct
7 Correct 3 ms 696 KB Output is correct
8 Correct 6 ms 1016 KB Output is correct
9 Correct 8 ms 1292 KB Output is correct
10 Correct 7 ms 1292 KB Output is correct
11 Correct 7 ms 1404 KB Output is correct
12 Correct 82 ms 6276 KB Output is correct
13 Correct 86 ms 6948 KB Output is correct
14 Correct 62 ms 7832 KB Output is correct
15 Correct 16 ms 7832 KB Output is correct
16 Correct 16 ms 7832 KB Output is correct
17 Correct 53 ms 7832 KB Output is correct
18 Correct 20 ms 7832 KB Output is correct
19 Correct 121 ms 9568 KB Output is correct
20 Correct 68 ms 9568 KB Output is correct
21 Runtime error 80 ms 17788 KB Execution killed with signal 11 (could be triggered by violating memory limits)