/* -*- coding: utf-8 -*- * * 2172.cc: No.2172 SEARCH in the Text Editor - yukicoder */ #include #include #include #include #include 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 vi; typedef pair pii; typedef vector vpii; /* typedef */ template 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 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; }