Submission #524162

# Submission time Handle Problem Language Result Execution time Memory
524162 2022-02-08T18:09:48 Z soeil Match (CEOI16_match) C++11
100 / 100
16 ms 11980 KB
#include <bits/stdc++.h>
using namespace std;

#define siz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define bit(n, k) (((n) >> (k)) & 1)

using ll = long long;

template <typename lhs> inline bool ckmin(lhs & a, lhs b) {
   return b < a ? a = b, true : false;
}

template <typename lhs> inline bool ckmax(lhs & a, lhs b) {
   return b > a ? a = b, true : false;
}

#define cint(c) (c - 'a')

const int maxn = 1e5 + 10, maxc = 28;
int n, dp[maxn][maxc];
string s, res;

void solve(int l, int r) {
   int p = dp[r][cint(s[l])];
   assert(p != 0 - 1);
   res[l] = '(';
   res[p] = ')';
   if (p + 1 < r) {
      solve(p + 1, r);
   }
   if (p - 1 > l + 1) {
      solve(l + 1, p - 1);
   }
}

void Main() {
   cin >> s;
   n = siz(s);
   res = s;
   stack <char> arr;
   for (int i = 0; i < n; i++) {
      if (arr.empty()) {
         arr.push(s[i]);
         continue;
      }
      if (arr.top() == s[i]) {
         arr.pop();
      } else {
         arr.push(s[i]);
      }
   }
   if (siz(arr)) {
      cout << "-1\n";
      return;
   }
   memset(dp, 511, sizeof dp);
   for (int i = 0; i < n; i++) {
      dp[i][cint(s[i])] = i;
      // cout << i << ' ' << cint(s[i]) << " -> " << dp[i][cint(s[i])] << '\n';
      if (i) {
         for (int c = 0; c < maxc; c++) {
            if (c == cint(s[i])) {
               continue;
            }
            int p = dp[i - 1][cint(s[i])];
            if (p > 0) {
               dp[i][c] = dp[p - 1][c];
            }
         }
      }
   }
   solve(0, n - 1);
   cout << res << '\n';
}

signed main() {
   ios_base::sync_with_stdio(false); cin.tie(0);
   int xyz = 1;
   // cin >> xyz;
   while (xyz--) {
      Main();
   }
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 4 ms 11212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 4 ms 11212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 4 ms 11212 KB Output is correct
5 Correct 4 ms 11196 KB Output is correct
6 Correct 5 ms 11212 KB Output is correct
7 Correct 5 ms 11212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 4 ms 11212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 4 ms 11212 KB Output is correct
5 Correct 4 ms 11196 KB Output is correct
6 Correct 5 ms 11212 KB Output is correct
7 Correct 5 ms 11212 KB Output is correct
8 Correct 5 ms 11204 KB Output is correct
9 Correct 5 ms 11204 KB Output is correct
10 Correct 5 ms 11196 KB Output is correct
11 Correct 5 ms 11300 KB Output is correct
12 Correct 10 ms 11596 KB Output is correct
13 Correct 9 ms 11516 KB Output is correct
14 Correct 9 ms 11724 KB Output is correct
15 Correct 10 ms 11540 KB Output is correct
16 Correct 10 ms 11596 KB Output is correct
17 Correct 11 ms 11724 KB Output is correct
18 Correct 16 ms 11588 KB Output is correct
19 Correct 13 ms 11844 KB Output is correct
20 Correct 10 ms 11596 KB Output is correct
21 Correct 13 ms 11980 KB Output is correct