結果
| 問題 | No.2172 SEARCH in the Text Editor |
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2022-12-18 20:23:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 105 ms / 2,000 ms |
| コード長 | 2,952 bytes |
| 記録 | |
| コンパイル時間 | 794 ms |
| コンパイル使用メモリ | 84,772 KB |
| 最終ジャッジ日時 | 2025-02-09 16:28:14 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 49 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;
vector<int> ZAlgorithm(const string& S) {
int n = S.size();
vector<int> res(n, 0);
res[0] = n;
int pbegin = 1, pend = 1;
for (int i = 1; i < n; i++) {
if (i < pend) {
res[i] = res[i - pbegin];
if (pend > i + res[i]) continue;
res[i] = pend - i;
pbegin = i;
}
else {
pbegin = pend = i;
}
while (true) {
if (pend == n) break;
if (S[pend] != S[res[i]]) break;
res[i]++;
pend++;
}
}
return res;
}
int countT(const string& s, const string& t){
auto z = ZAlgorithm(t + s);
int ans = 0;
for(size_t i=t.size(); i<z.size(); i++) if(z[i] >= (int)t.size()) ans++;
return ans;
}
const int MOD = 998244353;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
string T; cin >> T;
vector<long long> dp(N);
vector<string> pre(N);
vector<string> suf(N);
vector<bool> tooshort(N, false);
for(int i=0; i<N; i++){
string key; cin >> key;
if(key == "~"){
int j, k; cin >> j >> k; j--; k--;
if(tooshort[j]){
if(tooshort[k]){
string q = pre[j] + pre[k];
if(q.size() < T.size()){
pre[i] = q;
tooshort[i] = true;
}
else{
pre[i] = q.substr(0, T.size()-1);
suf[i] = q.substr(q.size() - T.size() + 1);
dp[i] = countT(q, T);
}
}
else{
string q = pre[j] + pre[k];
pre[i] = q.substr(0, T.size()-1);
suf[i] = suf[k];
dp[i] = (countT(q, T) + dp[k]) % MOD;
}
}
else{
if(tooshort[k]){
string q = suf[j] + pre[k];
pre[i] = pre[j];
suf[i] = q.substr(q.size() - T.size() + 1);
dp[i] = (countT(q, T) + dp[j]) % MOD;
}
else{
pre[i] = pre[j];
suf[i] = suf[k];
dp[i] = (dp[j] + dp[k] + countT(suf[j] + pre[k], T)) % MOD;
}
}
}
else{
if(key.size() < T.size()){
pre[i] = key;
tooshort[i] = true;
}
else{
pre[i] = key.substr(0, T.size() - 1);
suf[i] = key.substr(key.size() - T.size() + 1);
dp[i] = countT(key, T);
tooshort[i] = false;
}
}
}
cout << dp[N-1] << '\n';
return 0;
}
Nachia