Submission #556690

#TimeUsernameProblemLanguageResultExecution timeMemory
556690Soumya1Match (CEOI16_match)C++17
100 / 100
10 ms12400 KiB
#include <bits/stdc++.h> #ifdef __LOCAL__ #include <debug_local.h> #endif using namespace std; const int mxN = 1e5 + 5; int to[mxN][26]; string ans; string s; bool solve(int l, int r) { if (l > r) return true; if (l == r) return false; if (to[r][s[l] - 'a'] <= l) return false; ans[l] = '(', ans[to[r][s[l] - 'a']] = ')'; bool ok = solve(l + 1, to[r][s[l] - 'a'] - 1); ok &= solve(to[r][s[l] - 'a'] + 1, r); return ok; } void testCase() { cin >> s; int n = s.size(); ans = string(n, 'a'); for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) { to[i][j] = -1; } to[i][s[i] - 'a'] = i; if (i == 0) continue; int x = to[i - 1][s[i] - 'a']; if (x <= 0) continue; for (int j = 0; j < 26; j++) { if (j == s[i] - 'a') continue; to[i][j] = to[x - 1][j]; } } bool ok = solve(0, n - 1); cout << (!ok ? "-1" : ans) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); testCase(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...