답안 #57205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57205 2018-07-14T09:17:47 Z RezwanArefin01 괄호 문자열 (CEOI16_match) C++17
100 / 100
21 ms 10028 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii; 

const int N = 1e5 + 10;
int trie[N][26], n, p[N], u, idx, ed[N];  
vector<int> v[N]; 
char s[N], ans[N]; 

int cnt = 0; 
void solve(int l, int r) {
	if(l > r) return; 
	vector<int> &x = v[ed[l + 1]];
	auto it = --lower_bound(x.begin(), x.end(), r + 1);
	ans[l] = '('; 
	ans[*it] = ')'; 
	solve(l + 1, *it - 1); 
	solve(*it + 1, r); 
}
int main(int argc, char const *argv[]) {
	scanf(" %s", s); n = strlen(s);
	stack<int> st;
	for(int i = 0; i < n; i++) {
		int c = s[i] - 'a'; 
		if(!st.size() || st.top() != c) {
			st.push(c); 
			int &v = trie[u][c];
			if(!v) v = ++idx; 
			p[v] = u; 
			u = v; 
		} else {
			st.pop(); 
			u = p[u]; 
		} ed[i + 1] = u; 
	}
	for(int i = 0; i <= n; i++) {
		v[ed[i]].push_back(i); 
	}
	if(st.size()) return 0 * puts("-1"); 
	solve(0, n - 1); 
	for(int i = 0; i < n; i++)
		putchar(ans[i]); 
}

Compilation message

match.cpp: In function 'int main(int, const char**)':
match.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %s", s); n = strlen(s);
  ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 5 ms 2940 KB Output is correct
5 Correct 4 ms 2940 KB Output is correct
6 Correct 4 ms 2940 KB Output is correct
7 Correct 4 ms 2940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2788 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 5 ms 2940 KB Output is correct
5 Correct 4 ms 2940 KB Output is correct
6 Correct 4 ms 2940 KB Output is correct
7 Correct 4 ms 2940 KB Output is correct
8 Correct 5 ms 3244 KB Output is correct
9 Correct 5 ms 3272 KB Output is correct
10 Correct 5 ms 3416 KB Output is correct
11 Correct 5 ms 3476 KB Output is correct
12 Correct 13 ms 7716 KB Output is correct
13 Correct 14 ms 8216 KB Output is correct
14 Correct 15 ms 8616 KB Output is correct
15 Correct 14 ms 8616 KB Output is correct
16 Correct 12 ms 8616 KB Output is correct
17 Correct 15 ms 8616 KB Output is correct
18 Correct 10 ms 8616 KB Output is correct
19 Correct 21 ms 10028 KB Output is correct
20 Correct 17 ms 10028 KB Output is correct
21 Correct 21 ms 10028 KB Output is correct