Submission #116856

#TimeUsernameProblemLanguageResultExecution timeMemory
116856minhtung0404Match (CEOI16_match)C++17
100 / 100
20 ms12288 KiB
//https://oj.uz/problem/view/CEOI16_match #include<bits/stdc++.h> const int N = 1e5 + 5; using namespace std; string s, x; int n, nxt[N][26], f[N]; void solve(int l, int r){ if (l > r) return; int mat = nxt[r][s[l] - 'a']; x[l] = '('; x[mat] = ')'; solve(l+1, mat-1); solve(mat+1, r); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; n = s.size(); for (int i = 0; i < n; i++){ if (i == 0) f[i] = -1; else f[i] = nxt[i-1][s[i] - 'a']; for (int j = 0; j < 26; j++){ if (s[i] - 'a' == j) nxt[i][j] = i; else{ if (f[i] > 0) nxt[i][j] = nxt[f[i]-1][j]; else nxt[i][j] = -1; } } } stack <int> ms; for (int i = 0; i < n; i++){ if (ms.size() && ms.top() == s[i] - 'a') ms.pop(); else ms.push(s[i] - 'a'); } if (ms.size()) return cout << -1, 0; x = s; solve(0, n-1); cout << x; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...