제출 #1156755

#제출 시각아이디문제언어결과실행 시간메모리
1156755_callmelucian괄호 문자열 (CEOI16_match)C++17
100 / 100
11 ms12868 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pl = pair<ll,ll>; using pii = pair<int,int>; using tpl = tuple<int,int,int>; #define all(a) a.begin(), a.end() #define filter(a) a.erase(unique(all(a)), a.end()) const int mn = 1e5 + 5; int prv[26][mn], n; char bracket[mn]; string s; void solve (int l, int r) { if (l > r) return; int optMatch = prv[s[l] - 'a'][r]; if (s[l] != s[optMatch]) { cout << "Wrong " << l << " " << optMatch; exit(0); } bracket[l] = '(', bracket[optMatch] = ')'; solve(l + 1, optMatch - 1), solve(optMatch + 1, r); } bool tryMatch() { stack<char> st; for (int i = 1; i <= n; i++) { if (st.size() && st.top() == s[i]) st.pop(); else st.push(s[i]); } return st.empty(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.size(), s = " " + s; if (!tryMatch()) return cout << -1, 0; for (int i = 1; i <= n; i++) { int cur = s[i] - 'a'; prv[cur][i] = i; if (prv[cur][i - 1] > 1) { int pos = prv[cur][i - 1] - 1; for (int j = 0; j < 26; j++) if (j != cur) prv[j][i] = prv[j][pos]; } } solve(1, n); for (int i = 1; i <= n; i++) cout << bracket[i]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...