結果

問題 No.2172 SEARCH in the Text Editor
ユーザー tnakao0123tnakao0123
提出日時 2022-12-27 14:23:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 2,380 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 59,904 KB
実行使用メモリ 51,328 KB
最終ジャッジ日時 2024-11-22 00:03:04
合計ジャッジ時間 6,262 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2172.cc:  No.2172 SEARCH in the Text Editor - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 50;
const int MAX_L = 100000;
const int MOD = 998244353;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }
};

typedef MI<MOD> mi;

/* global variables */

char t[MAX_M + 4], s[MAX_L + 4];
mi ks[MAX_N];
vi hms[MAX_N], tms[MAX_N];
vpii mms[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d%s", &n, t);
  int m = strlen(t);

  for (int i = 0; i < n; i++) {
    scanf("%s", s);

    if (s[0] == '~') {
      int i0, i1;
      scanf("%d%d", &i0, &i1);
      i0--, i1--;

      ks[i] = ks[i0] + ks[i1];
      hms[i] = hms[i1];
      tms[i] = tms[i0];

      for (auto hj: hms[i0])
	for (auto tj: tms[i1])
	  if (hj == tj) ks[i] += 1;

      for (auto hj: hms[i0])
	for (auto me: mms[i1])
	  if (hj == me.first) hms[i].push_back(me.second);
      for (auto tj: tms[i1])
	for (auto me: mms[i0])
	  if (tj == me.second) tms[i].push_back(me.first);
	for (auto me0: mms[i0])
	  for (auto me1: mms[i1])
	    if (me0.second == me1.first)
	      mms[i].push_back(pii(me0.first, me1.second));
    }
    else {
      int l = strlen(s);
      for (int j = 0; j + m <= l; j++)
	if (! strncmp(s + j, t, m)) ks[i] += 1;

      for (int j = 1; j < m; j++) {
	if (j <= l && ! strncmp(s + l - j, t, j))
	  hms[i].push_back(j);
	if (m - j <= l && ! strncmp(s, t + j, m - j))
	  tms[i].push_back(j);
	if (j + l < m && ! strncmp(t + j, s, l))
	  mms[i].push_back(pii(j, j + l));
      }
    }
  }

  printf("%d\n", ks[n - 1].v);

  return 0;
}
0