답안 #524221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524221 2022-02-08T20:14:31 Z soeil 괄호 문자열 (CEOI16_match) C++11
100 / 100
13 ms 11976 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])];
   if (p == 0 - 1) {
      cout << "-1\n";
      exit(0);
   }
   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;
   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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 5 ms 11196 KB Output is correct
3 Correct 5 ms 11212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 5 ms 11196 KB Output is correct
3 Correct 5 ms 11212 KB Output is correct
4 Correct 5 ms 11212 KB Output is correct
5 Correct 5 ms 11212 KB Output is correct
6 Correct 5 ms 11212 KB Output is correct
7 Correct 5 ms 11196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11204 KB Output is correct
2 Correct 5 ms 11196 KB Output is correct
3 Correct 5 ms 11212 KB Output is correct
4 Correct 5 ms 11212 KB Output is correct
5 Correct 5 ms 11212 KB Output is correct
6 Correct 5 ms 11212 KB Output is correct
7 Correct 5 ms 11196 KB Output is correct
8 Correct 5 ms 11212 KB Output is correct
9 Correct 5 ms 11208 KB Output is correct
10 Correct 5 ms 11212 KB Output is correct
11 Correct 5 ms 11200 KB Output is correct
12 Correct 9 ms 11596 KB Output is correct
13 Correct 13 ms 11596 KB Output is correct
14 Correct 10 ms 11704 KB Output is correct
15 Correct 11 ms 11544 KB Output is correct
16 Correct 11 ms 11596 KB Output is correct
17 Correct 11 ms 11596 KB Output is correct
18 Correct 11 ms 11596 KB Output is correct
19 Correct 12 ms 11768 KB Output is correct
20 Correct 9 ms 11596 KB Output is correct
21 Correct 13 ms 11976 KB Output is correct