結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2020-06-07 04:19:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 357 ms / 2,000 ms |
コード長 | 1,263 bytes |
コンパイル時間 | 2,441 ms |
コンパイル使用メモリ | 204,168 KB |
最終ジャッジ日時 | 2025-01-10 23:36:49 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; class rolling_hash{ static const long long base=10007,mod1=1e9+7,mod2=1e9+9; int len; vector<long long> pow1,pow2,h1,h2; public: using hash_type=pair<long long,long long>; rolling_hash(const string& s):len(s.length()),pow1(len+1),pow2(len+1),h1(len+1),h2(len+1){ pow1[0]=pow2[0]=1; rep(i,len){ pow1[i+1]=pow1[i]*base%mod1; pow2[i+1]=pow2[i]*base%mod2; h1[i+1]=(h1[i]*base+s[i])%mod1; h2[i+1]=(h2[i]*base+s[i])%mod2; } } hash_type get_hash(int l,int r)const{ assert(0<=l && l<=r && r<=len); auto res1=(h1[r]-h1[l]*pow1[r-l])%mod1; if(res1<0) res1+=mod1; auto res2=(h2[r]-h2[l]*pow2[r-l])%mod2; if(res2<0) res2+=mod2; return {res1,res2}; } static hash_type get_hash(const string& s){ long long res1=0,res2=0; rep(i,s.length()){ res1=(res1*base+s[i])%mod1; res2=(res2*base+s[i])%mod2; } return {res1,res2}; } }; int main(){ string s; cin>>s; int n=s.length(); rolling_hash H(s); map<rolling_hash::hash_type,int> f; rep(d,min(n,10)+1) rep(i,n-d+1) ++f[H.get_hash(i,i+d)]; int ans=0,q; cin>>q; rep(_,q){ string t; cin>>t; ans+=f[rolling_hash::get_hash(t)]; } cout<<ans<<'\n'; return 0; }