結果
| 問題 |
No.2761 Substitute and Search
|
| コンテスト | |
| ユーザー |
srjywrdnprkt
|
| 提出日時 | 2024-10-21 19:53:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,282 bytes |
| コンパイル時間 | 2,415 ms |
| コンパイル使用メモリ | 206,532 KB |
| 最終ジャッジ日時 | 2025-02-24 22:12:33 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 1 TLE * 1 -- * 11 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937_64 rng(time(0));
ll MOD[3] = {998244353, 1000000007, 1000000021};
ll BASE[3] = {static_cast<ll>(rng() % MOD[0]), static_cast<ll>(rng() % MOD[1]), static_cast<ll>(rng() % MOD[2]) };
//Rolling Hash on Segment Tree
struct Record{
ll hash, pow;
};
template<class S, S (*op)(S, S), S (*e)()>struct SegTree {
private:
vector<S> seg; int N = 1; int n;
public:
SegTree (ll n) : SegTree(vector<S>(n, e())) {}
SegTree (const vector<S> &v){
n = v.size(); while (N < n) N *= 2; seg.resize(N*2-1, e());
for (ll i=0; i<n; i++) seg[i+N-1] = v[i];
for (ll i=N-2; i>=0; i--) seg[i] = op(seg[i*2+1], seg[i*2+2]);
}
void set(int loc, S val){
loc += N-1; seg[loc] = val;
while (loc != 0){
loc--;
loc >>= 1;
seg[loc] = op(seg[(loc<<1)+1], seg[(loc<<1)+2]);
}
}
S prod (int l, int r) const{
l += N-1; r += N-1;
S pl = e(), pr = e();
while(l<=r){
if (!(l & 1)){
pl = op(pl, seg[l]); l++;
}
if (r & 1){
pr = op(seg[r], pr); r--;
}
l--; r--; l >>= 1; r >>= 1;
}
return op(pl, pr);
}
S all_prod() const {return seg[0];}
S get (int i) const {return seg[i+N-1];}
};
using S = array<Record, 3>;
S op(S a, S b){
S res;
for (int i=0; i<3; i++){
res[i] = {(a[i].hash * b[i].pow % MOD[i] + b[i].hash) % MOD[i], (a[i].pow * b[i].pow)%MOD[i]};
}
return res;
}
S e() {
S res;
for (int i=0; i<3; i++) res[i] = {0,1};
return res;
}
S gen(int x){
S res;
for (int i=0; i<3; i++) res[i] = {x, BASE[i]};
return res;
}
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
/*
tがSの接頭辞であるか?
t[1:|T|]=S[1:|T|]
Rolling Hash
*/
ll N, L, Q, t, k;
char c, d;
string T;
cin >> N >> L >> Q;
vector<string> s(N);
vector<SegTree<S, op, e>> tree;
auto to_hash=[&](string &s){
S x;
int N=s.size();
vector<S> v(N);
for (int i=0; i<N; i++){
x = gen(s[i]-'a');
v[i] = x;
}
return SegTree<S, op, e>(v);
};
for (int i=0; i<N; i++){
cin >> s[i];
tree.push_back(to_hash(s[i]));
};
while(Q--){
cin >> t;
if (t == 1){
cin >> k >> c >> d;
k--;
for (int i=0; i<N; i++){
if (s[i][k] == c){
s[i][k] = d;
tree[i].set(k, gen(d-'a'));
}
}
}
else{
cin >> T;
int ans=0;
SegTree<S, op, e> tree2 = to_hash(T);
S x = tree2.all_prod();
for (int i=0; i<N; i++){
bool f=1;
S y = tree[i].prod(0, T.size()-1);
for (int j=0; j<3; j++) f &= (x[j].hash == y[j].hash);
ans += f;
}
cout << ans << endl;
}
}
return 0;
}
srjywrdnprkt