Submission #1117829

#TimeUsernameProblemLanguageResultExecution timeMemory
1117829AtabayRajabliMatch (CEOI16_match)C++17
10 / 100
2 ms4688 KiB
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define int long long using namespace std; const int sz = 1e5 + 5; string s, t; int n, last[26][sz]; void f(int l, int r) { if(l > r) return; int pos = last[s[l] - 'a'][r]; t[l] = '(', t[pos] = ')'; f(l + 1, pos - 1), f(pos + 1, r); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.size(); stack<int> st; for(int i = 0; i < n; i++) { if(!st.empty() && s[st.top()] == s[i]) { st.pop(); } else { st.push(i); } } if(!st.empty()) { cout << -1; return 0; } for(int i = 0; i < n; i++) { int l = s[i] - 'a'; last[l][i] = i; if(i == 0) continue; for(int j = 0; j < 26; j++) { if(last[j][i - 1] >= 0 && j != l) last[j][i] = last[j][i - 1]; } } t.resize(n); f(0, n - 1); cout << t; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...