Submission #556689

#TimeUsernameProblemLanguageResultExecution timeMemory
556689Soumya1Match (CEOI16_match)C++17
10 / 100
0 ms340 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] = (i == 0 ? -1 : to[i - 1][j]); } to[i][s[i] - 'a'] = i; } 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...