結果

問題 No.2172 SEARCH in the Text Editor
ユーザー norikamenorikame
提出日時 2022-12-24 01:22:25
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,376 bytes
コンパイル時間 5,917 ms
コンパイル使用メモリ 326,156 KB
実行使用メモリ 386,560 KB
最終ジャッジ日時 2024-04-29 06:08:13
合計ジャッジ時間 9,881 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);
const ll mod = 998244353LL;

int main() {
	int n;
	string t;
	cin >> n >> t;
	int tlen = t.length();
	vector<string> s(n);
	vector<int> js(n, -1), ks(n, -1), lcnt(n);
	rep(i, n) {
		cin >> s[i];
		if (s[i] != "~") {
			lcnt[i] = s[i].length();
			continue;
		}
		cin >> js[i] >> ks[i];
		js[i]--; ks[i]--;
		if (lcnt[js[i]]+lcnt[ks[i]] <= tlen*2) {
			s[i] = s[js[i]] + s[ks[i]];
			lcnt[i] = s[i].length();
			continue;
		}
		lcnt[i] = min(INF, lcnt[js[i]]+lcnt[ks[i]]);
	}
	map<pair<int, int>, bool> bmemo, fmemo;
	unordered_map<int, ll> memo;
	auto bcheck = [&](auto bcheck, int sid, int plen) -> bool {
		if (bmemo.find({ sid, plen }) != bmemo.end()) return bmemo[{sid, plen}];
		if (plen == 0) return bmemo[{sid, plen}] = true;
		if (s[sid] != "~") {
			if (lcnt[sid] < plen) return bmemo[{sid, plen}] = false;
			return bmemo[{sid, plen}] = (s[sid].substr(lcnt[sid]-plen) == t.substr(0,plen));
		}
		else return bmemo[{sid, plen}] = bcheck(bcheck, ks[sid], plen);
	};
	auto fcheck = [&](auto fcheck, int sid, int plen) -> bool {
		if (fmemo.find({ sid, plen }) != fmemo.end()) return fmemo[{sid, plen}];
		if (plen == 0) return fmemo[{sid, plen}] = true;
		if (s[sid] != "~") {
			if (lcnt[sid] < plen) return fmemo[{sid, plen}] = false;
			return fmemo[{sid, plen}] = (s[sid].substr(0,plen) == t.substr(tlen-plen));
		}
		else return fmemo[{sid, plen}] = fcheck(fcheck, js[sid], plen);
	};
	auto calc = [&](auto calc, int sid) -> ll {
		if (memo.find(sid) != memo.end()) return memo[sid];
		ll rval = 0;
		if (lcnt[sid] < tlen) return memo[sid] = rval;
		if (s[sid] != "~") {
			for (int i=0; i+tlen-1<lcnt[sid]; ++i) if (s[sid].substr(i,tlen) == t) ++rval;
			return memo[sid] = rval;
		}
		else {
			rep3(i, 1, tlen) if (bcheck(bcheck, js[sid], tlen-i) && fcheck(fcheck, ks[sid], i)) ++rval;
			return memo[sid] = (rval + calc(calc, js[sid]) + calc(calc, ks[sid])) % mod;
		}
	};
	ll res = calc(calc, n-1);
	cout << res << endl;
	return 0;
}
0