結果

問題 No.2172 SEARCH in the Text Editor
ユーザー KudeKude
提出日時 2022-12-24 01:45:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 2,318 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 230,128 KB
実行使用メモリ 31,812 KB
最終ジャッジ日時 2023-08-11 13:46:16
合計ジャッジ時間 6,547 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 94 ms
31,736 KB
testcase_04 AC 102 ms
31,680 KB
testcase_05 AC 95 ms
31,720 KB
testcase_06 AC 15 ms
6,716 KB
testcase_07 AC 13 ms
5,764 KB
testcase_08 AC 14 ms
5,924 KB
testcase_09 AC 27 ms
8,252 KB
testcase_10 AC 25 ms
8,028 KB
testcase_11 AC 16 ms
6,284 KB
testcase_12 AC 19 ms
6,928 KB
testcase_13 AC 20 ms
7,724 KB
testcase_14 AC 29 ms
8,832 KB
testcase_15 AC 18 ms
6,720 KB
testcase_16 AC 19 ms
7,332 KB
testcase_17 AC 17 ms
6,776 KB
testcase_18 AC 29 ms
9,364 KB
testcase_19 AC 22 ms
8,460 KB
testcase_20 AC 13 ms
5,912 KB
testcase_21 AC 28 ms
9,648 KB
testcase_22 AC 32 ms
9,860 KB
testcase_23 AC 36 ms
9,756 KB
testcase_24 AC 36 ms
10,092 KB
testcase_25 AC 36 ms
9,888 KB
testcase_26 AC 94 ms
31,732 KB
testcase_27 AC 91 ms
31,800 KB
testcase_28 AC 28 ms
9,892 KB
testcase_29 AC 29 ms
9,892 KB
testcase_30 AC 31 ms
9,892 KB
testcase_31 AC 31 ms
9,832 KB
testcase_32 AC 32 ms
10,024 KB
testcase_33 AC 32 ms
9,764 KB
testcase_34 AC 34 ms
9,888 KB
testcase_35 AC 35 ms
9,772 KB
testcase_36 AC 35 ms
9,768 KB
testcase_37 AC 36 ms
9,824 KB
testcase_38 AC 111 ms
31,812 KB
testcase_39 AC 111 ms
31,772 KB
testcase_40 AC 30 ms
9,892 KB
testcase_41 AC 31 ms
9,828 KB
testcase_42 AC 31 ms
9,548 KB
testcase_43 AC 31 ms
9,508 KB
testcase_44 AC 31 ms
9,816 KB
testcase_45 AC 29 ms
9,888 KB
testcase_46 AC 22 ms
7,620 KB
testcase_47 AC 16 ms
6,392 KB
testcase_48 AC 17 ms
6,540 KB
testcase_49 AC 26 ms
9,072 KB
testcase_50 AC 24 ms
8,604 KB
testcase_51 AC 14 ms
6,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

template <class T>
vector<int> KMP(const T& pattern) {
  const int n = pattern.size();
  vector<int> pi(n + 1);
  pi[0] = -1;
  int border = 0;
  for (int i = 1; i < n; i++, border++) {
    const auto c = pattern[i];
    if (pattern[border] == c) {
      pi[i] = pi[border];
    } else {
      pi[i] = border;
      while (border != -1 && pattern[border] != c) {
        border = pi[border];
      }
    }
  }
  pi[n] = border;
  return pi;
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string t;
  cin >> t;
  int m = t.size();
  VI kmp = KMP(t);
  vector<string> head(n), tail(n);
  vector<mint> cnt(n);
  rep(i, n) {
    string s;
    cin >> s;
    if (s[0] == '~') {
      int j, k;
      cin >> j >> k;
      j--, k--;
      cnt[i] = cnt[j] + cnt[k];
      int p = 0;
      for(char c: tail[j]) {
        while(p != -1 && t[p] != c) p = kmp[p];
        p++;
        if (p == m) p = kmp[p];
      }
      for(char c: head[k]) {
        while(p != -1 && t[p] != c) p = kmp[p];
        p++;
        if (p == m) cnt[i]++, p = kmp[p];
      }
      head[i] = head[j] + head[k];
      tail[i] = tail[j] + tail[k];
    } else {
      int p = 0;
      for(char c: s) {
        while(p != -1 && t[p] != c) p = kmp[p];
        p++;
        if (p == m) cnt[i]++, p = kmp[p];
      }
      head[i] = s;
      tail[i] = s;
    }
    if (int(head[i].size()) >= m) head[i].resize(m - 1);
    if (int(tail[i].size()) >= m) tail[i].erase(tail[i].begin(), tail[i].end() - (m - 1));
  }
  cout << cnt[n - 1].val() << '\n';
}
0