Submission #116523

#TimeUsernameProblemLanguageResultExecution timeMemory
116523IOrtroiiiMatch (CEOI16_match)C++14
100 / 100
23 ms20096 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 100100;

char s[N];
int let[N];
int par[N];
int nxt[N][26];
int last[N][26];
int opt[N][26];
int tot;

void solve(int l, int r) {
   if (l > r) return;
   int opt = ::opt[r][s[l] - 'a'];
   if (opt <= l) { puts("-1"); exit(0); }
   s[l] = '(', s[opt] = ')';
   solve(l + 1, opt - 1);
   solve(opt + 1, r);
}

int main() {
   scanf("%s", s + 1);
   int n = strlen(s + 1);
   int cur = 0;
   for (int i = 1; i <= n; ++i) {
      if (let[cur] == s[i]) {
         cur = par[cur];
      } else {
         int &nxt = ::nxt[cur][s[i] - 'a'];
         if (!nxt) {
            nxt = ++tot;
            par[nxt] = cur;
            let[nxt] = s[i];
         }
         cur = nxt;
      }
      last[cur][s[i] - 'a'] = i;
      for (int j = 0; j < 26; ++j) {
         opt[i][j] = last[cur][j];
      }
   }
   solve(1, n);
   puts(s + 1);
}

Compilation message (stderr)

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