Submission #116852

#TimeUsernameProblemLanguageResultExecution timeMemory
116852minhtung0404Match (CEOI16_match)C++17
10 / 100
2051 ms384 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; x[l] = '('; int mat = nxt[r][s[l] - 'a']; x[mat] = ')'; solve(l+1, mat-1); solve(mat+1, r); } int main(){ cin >> s; n = s.size(); for (int i = 0; i < n; i++){ if (i > 0 && s[i-1] == s[i]) f[i] = i-1; else { if (i == 0) f[i] = -1; else f[i] = nxt[i-1][s[i] - 'a']; } for (int j = 0; j < 26; j++){ if (f[i] > 0) { if (s[i]-'a' != j) nxt[i][j] = nxt[f[i]-1][j]; else nxt[i][j] = i; } 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...