답안 #617954

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
617954 2022-08-01T17:48:39 Z GusterGoose27 괄호 문자열 (CEOI16_match) C++11
100 / 100
43 ms 29068 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5;

class Trie {
public:
	map<int, Trie*> nexttrie;
	Trie *par;
	map<int, vector<int>> occs;
	Trie(Trie *p = nullptr) {
		par = p;
	}
};

string s;
int n;
bool seq[MAXN];
int nums[MAXN];
int nextocc[MAXN+1][20]; // [l, r)
Trie *cur;
Trie *tries[MAXN];

void make_occ(Trie *t, int v) {
	t->occs[nums[v]].push_back(v);
}

void make(int l, int r) {
	if (r < l) return;
	cur = tries[l];
	int num = nums[l];
	assert(cur->occs[num].back() > l);
	int mn = 0;
	int mx = cur->occs[num].size();
	while (mx > mn+1) {
		int c = (mn+mx)/2;
		if (cur->occs[num][c] <= r) mn = c;
		else mx = c;
	}
	int rp = cur->occs[num][mn];
	assert(rp > l);
	seq[l] = 0;
	seq[rp] = 1;
	make(l+1, rp-1);
	make(rp+1, r);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cur = new Trie();
	cin >> s;
	n = s.size();
	for (int i = 0; i < n; i++) {
		nums[i] = s[i]-'a';
		nextocc[i][0] = i;
	}
	vector<int> stck;
	for (int i = 0; i < n; i++) {
		tries[i] = cur;
		if (!stck.empty() && nums[i] == nums[stck.back()]) {
			cur = cur->par;
			stck.pop_back();
		}
		else {
			if (cur->nexttrie.find(nums[i]) == cur->nexttrie.end()) cur->nexttrie[nums[i]] = new Trie(cur);
			cur = cur->nexttrie[nums[i]];
			stck.push_back(i);
		}
		make_occ(cur, i);
	}
	if (!stck.empty()) {
		cout << "-1\n";
		return 0;
	}
	nextocc[n][0] = n;
	for (int j = 1; j < 20; j++) {
		for (int i = 0; i <= n; i++) nextocc[i][j] = nextocc[nextocc[i][j-1]][j-1];
	}
	make(0, n-1);
	char convert[2];
	convert[0] = '(';
	convert[1] = ')';
	for (int i = 0; i < n; i++) cout << convert[seq[i]];
	cout << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 1752 KB Output is correct
9 Correct 2 ms 2132 KB Output is correct
10 Correct 3 ms 2388 KB Output is correct
11 Correct 3 ms 2516 KB Output is correct
12 Correct 22 ms 18968 KB Output is correct
13 Correct 23 ms 20564 KB Output is correct
14 Correct 26 ms 22492 KB Output is correct
15 Correct 14 ms 11860 KB Output is correct
16 Correct 15 ms 11860 KB Output is correct
17 Correct 25 ms 20176 KB Output is correct
18 Correct 16 ms 10828 KB Output is correct
19 Correct 35 ms 27952 KB Output is correct
20 Correct 24 ms 19648 KB Output is correct
21 Correct 43 ms 29068 KB Output is correct