답안 #609271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
609271 2022-07-27T13:10:14 Z AkramElOmrani 괄호 문자열 (CEOI16_match) C++17
100 / 100
25 ms 15804 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ios ios_base::sync_with_stdio(0);cin.tie(0);

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen(int a, int b) {return (ll)rng() % (b - a + 1) + a;}

const int alp = 26;

int main()
{
	ios
	string s; cin >> s;
	int n = s.length();
	stack<char> st;
	for(char c : s) {
		if(!st.empty() && st.top() == c) {
			st.pop();
		} else {
			st.push(c);
		}
	}
	if(!st.empty()) {
		cout << -1;
		return 0;
	}
	vector<vector<int>> after(n, vector<int>(alp));
	for(int i = 0; i < n; ++i) {
		int last = -1;
		if(i > 0) {
			last = after[i - 1][s[i] - 'a'];
		}
		for(int p = 0; p < alp; ++p) {
			if(s[i] - 'a' == p) after[i][p] = i;
			else {
				if(last > 0) {
					after[i][p] = after[last - 1][p];
				} else {
					after[i][p] = -1;
				}
			}
		}
	}
	function<void(int, int)> rec = [&](int l, int r) {
		if(l > r) return;
		int mid = after[r][s[l] - 'a'];
		// cas of s[l] - 'a' and both of them are equal
		s[l] = '(';
		s[mid] = ')';
		rec(l + 1, mid - 1), rec(mid + 1, r);
	};
	rec(0, n - 1);
	cout << s << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1364 KB Output is correct
10 Correct 2 ms 1364 KB Output is correct
11 Correct 1 ms 1364 KB Output is correct
12 Correct 11 ms 9428 KB Output is correct
13 Correct 12 ms 10204 KB Output is correct
14 Correct 17 ms 11384 KB Output is correct
15 Correct 14 ms 13012 KB Output is correct
16 Correct 20 ms 13076 KB Output is correct
17 Correct 19 ms 13748 KB Output is correct
18 Correct 17 ms 13304 KB Output is correct
19 Correct 25 ms 14612 KB Output is correct
20 Correct 12 ms 9812 KB Output is correct
21 Correct 22 ms 15804 KB Output is correct