結果
| 問題 |
No.2761 Substitute and Search
|
| コンテスト | |
| ユーザー |
nonon
|
| 提出日時 | 2024-05-18 16:14:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,028 bytes |
| コンパイル時間 | 2,489 ms |
| コンパイル使用メモリ | 211,240 KB |
| 最終ジャッジ日時 | 2025-02-21 15:45:15 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 1 MLE * 1 -- * 11 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
template<typename S>
struct dynamic_rolling_hash
{
using dat=array<long long,5>;
dynamic_rolling_hash():dynamic_rolling_hash(0,0){}
dynamic_rolling_hash(int _n, long long _b):dynamic_rolling_hash(vector<S>(_n),_b){}
dynamic_rolling_hash(const vector<S>&_v, long long _b):n(_v.size()),base(_b)
{
mod_pow.resize(n+1);
for(int j=0;j<5;j++)mod_pow[0][j]=1;
for(int i=0;i<n;i++)for(int j=0;j<5;j++)mod_pow[i+1][j]=(mod_pow[i][j]*base)%mod[j];
log=0;
while((1<<log)<n)log++;
sz=1<<log;
node.resize(2*sz,e());
for(int i=0;i<n;i++)for(int j=0;j<5;j++)
{
node[i+sz].first[j]=inner_hash(_v[i],j);
node[i+sz].second=1;
}
for(int i=sz-1;i>=1;i--)update(i);
}
void set(int p, S x)
{
p+=sz;
for(int j=0;j<5;j++)node[p].first[j]=inner_hash(x,j);
node[p].second=1;
for(int i=1;i<=log;i++)update(p>>i);
}
pair<dat,int> prod(int l, int r)
{
auto pl=e(),pr=e();
l+=sz,r+=sz;
while(l<r)
{
if(l&1)pl=op(pl,node[l++]);
if(r&1)pr=op(node[--r],pr);
l>>=1,r>>=1;
}
return op(pl,pr);
}
pair<dat,int> all_prod(){return node[1];}
private:
int n,log,sz;
long long base;
vector<pair<dat,int>>node;
const dat mod=dat{94663399,993272689,998700149,1000001087,1000100033};
vector<dat>mod_pow;
long long inner_hash(S x, int p){return (base^(base-x))%mod[p];}
pair<dat,int>op(const pair<dat,int>&a, const pair<dat,int>&b)
{
dat c;
for(int i=0;i<5;i++)c[i]=(a.first[i]+b.first[i]*mod_pow[a.second][i])%mod[i];
return make_pair(c,a.second+b.second);
}
pair<dat,int>e(){return make_pair(dat(),0);}
void update(int p){node[p]=op(node[2*p],node[2*p+1]);}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
const int base=rand()%(1<<15)+1;
int N,L,Q;
cin>>N>>L>>Q;
vector<string>S(N);
vector<dynamic_rolling_hash<int>>RH;
for(int i=0;i<N;i++)
{
cin>>S[i];
vector<int>v(L);
for(int j=0;j<L;j++)v[j]=S[i][j];
RH.push_back(dynamic_rolling_hash<int>(v,base));
}
while(Q--)
{
int t;
cin>>t;
if(t==1)
{
int p;
char c,d;
cin>>p>>c>>d;
p--;
for(int i=0;i<N;i++)if(S[i][p]==c)
{
S[i][p]=d;
RH[i].set(p,int(d));
}
}
else
{
string T;
cin>>T;
vector<int>v;
for(char c:T)v.push_back(int(c));
auto hashT=dynamic_rolling_hash(v,base).all_prod();
int ans=0;
for(int i=0;i<N;i++)
{
auto hashS=RH[i].prod(0,T.size());
if(hashS==hashT)ans++;
}
cout<<ans<<'\n';
}
}
}
nonon