Submission #127217

#TimeUsernameProblemLanguageResultExecution timeMemory
127217roseanne_pcyMatch (CEOI16_match)C++14
100 / 100
17 ms12892 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC target ("sse4") using namespace std; #define X first #define Y second #define pb push_back typedef pair<int, int> ii; typedef long long ll; const int maxn = 1e5+5; char S[maxn]; int n; int dp[maxn][30]; char ans[maxn]; bool valid() { stack<char> s; for(int i = 0; i< n; i++) { if(s.empty() || s.top() != S[i]) { s.push(S[i]); } else s.pop(); } return s.empty(); } void solve(int L, int R) { if(L> R) return; int opt = dp[R][S[L]-'a']; // printf("[%d %d] match %d\n", L, R, opt); assert(opt> L); ans[L] = '('; ans[opt] = ')'; solve(L+1, opt-1); solve(opt+1, R); } int main() { scanf("%s", S); n = strlen(S); if(!valid()) { printf("-1\n"); return 0; } for(int j = 0; j< 26; j++) { if(S[0] == 'a'+j) dp[0][j] = 0; else dp[0][j] = -1; } for(int i = 1; i< n; i++) { int x = dp[i-1][S[i]-'a']; // printf("x = %d\n", x); for(int j = 0; j< 26; j++) { if(S[i] == 'a'+j) dp[i][j] = i; else if(x >= 1) dp[i][j] = dp[x-1][j]; else dp[i][j] = -1; // printf("dp[%d][%d] = %d\n", i, j, dp[i][j]); } } solve(0, n-1); ans[n] = '\0'; printf("%s\n", ans); }

Compilation message (stderr)

match.cpp: In function 'int main()':
match.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", S);
  ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...