답안 #680345

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680345 2023-01-10T15:44:15 Z peijar 괄호 문자열 (CEOI16_match) C++17
100 / 100
38 ms 38676 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int i = 0; i < (int)vec.size(); ++i) {
    out << vec[i];
    if (i + 1 < (int)vec.size())
      out << ", ";
  }
  return out << "]";
}
} // namespace std

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << H;
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int MAXP = 20;
const int MAXN = 1e5;
const int ALPHA = 26;

int nxtGood[MAXN][ALPHA];
int jmp[MAXP][MAXN];

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int N = s.size();

  vector<int> nxt(N);
  for (int i = N - 1; i >= 0; --i) {
    nxt[i] = i + 1;
    while (nxt[i] < N and s[nxt[i]] != s[i])
      nxt[i] = nxt[nxt[i]] + 1;
    if (nxt[i] > N)
      nxt[i] = N;
  }
  for (int i = 0; i < N; ++i) {
    nxt[i]++;
    if (nxt[i] == N + 1)
      nxt[i] = -1;
  }
  dbg(nxt);
  int pos = 0;
  while (pos != -1 and pos < N)
    pos = nxt[pos];
  if (pos == -1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < N; ++i)
    jmp[0][i] = nxt[i];
  for (int p = 0; p < MAXP - 1; ++p)
    for (int i = 0; i < N; ++i) {
      int j = jmp[p][i];
      if (j == N)
        j = -1;
      if (j != -1)
        j = jmp[p][j];
      jmp[p + 1][i] = j;
    }

  for (int i = N - 1; i >= 0; --i) {
    for (int alpha = 0; alpha < ALPHA; ++alpha)
      nxtGood[i][alpha] =
          nxt[i] == -1 or nxt[i] == N ? N : nxtGood[nxt[i]][alpha];
    nxtGood[i][s[i] - 'a'] = i;
  }

  string ret(N, '.');

  auto Solve = [&](auto solve, int deb, int fin) {
    dbg(deb, fin);
    assert((fin - deb) % 2 == 0);
    if (deb == fin)
      return;
    int cur = deb + 1;
    for (int p = MAXP - 1; p >= 0; --p)
      if (jmp[p][cur] != -1 and jmp[p][cur] != N and
          nxtGood[jmp[p][cur]][s[deb] - 'a'] < fin) {
        cur = jmp[p][cur];
      }
    int split = nxtGood[cur][s[deb] - 'a'];
    dbg(cur, split);
    ret[deb] = '(';
    ret[split] = ')';
    solve(solve, deb + 1, split);
    solve(solve, split + 1, fin);
  };
  Solve(Solve, 0, N);
  cout << ret << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 3020 KB Output is correct
10 Correct 4 ms 3028 KB Output is correct
11 Correct 2 ms 3156 KB Output is correct
12 Correct 18 ms 23520 KB Output is correct
13 Correct 21 ms 25460 KB Output is correct
14 Correct 29 ms 27596 KB Output is correct
15 Correct 38 ms 31648 KB Output is correct
16 Correct 31 ms 31692 KB Output is correct
17 Correct 23 ms 33620 KB Output is correct
18 Correct 33 ms 34472 KB Output is correct
19 Correct 32 ms 36648 KB Output is correct
20 Correct 17 ms 23628 KB Output is correct
21 Correct 30 ms 38676 KB Output is correct