Submission #681395

#TimeUsernameProblemLanguageResultExecution timeMemory
681395vjudge1Match (CEOI16_match)C++14
10 / 100
2083 ms19576 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define pb push_back #define all(a) a.begin(), a.end() typedef long long ll; typedef pair<int, int> ii; const int N = 2000 + 5; const int mod = 1e9 + 7; unordered_map<int, bool> f; string s; string t; int n; int encode(int a, int b) { return a * 1e5 + b; } int dp(int l, int r) { if(l > r) return 1; if((r - l + 1) & 1) return 0; auto it = f.insert({encode(l, r), 0}); if(it.S == 0) return it.F -> S; bool &res = it.F -> S; if(s[l] == s[r]) res |= dp(l + 1, r - 1); for(int i = r - 1; i > l; i--) { if(s[l] == s[i]) res |= (dp(l + 1, i - 1) && dp(i + 1, r)); if(res) break; } return res; } void trace(int l, int r) { if(l > r) return; if(s[l] == s[r]) { t[l] = '(', t[r] = ')'; trace(l + 1, r - 1); return; } for(int i = r - 1; i > l; i--) if(s[l] == s[i] && (dp(l + 1, i - 1) && dp(i + 1, r))) { t[l] = '(', t[i] = ')'; trace(l + 1, i - 1); trace(i + 1, r); return; } } void solve() { cin >> s; int n = s.size(); t.resize(n); if(dp(0, n - 1)) { trace(0, n - 1); cout << t; } else cout << -1; } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...