結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  xenon_motsu | 
| 提出日時 | 2019-08-12 11:33:45 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 843 ms / 2,000 ms | 
| コード長 | 2,490 bytes | 
| コンパイル時間 | 1,557 ms | 
| コンパイル使用メモリ | 170,664 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:27:33 | 
| 合計ジャッジ時間 | 10,322 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ft first
#define sd second
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)
template <uint_fast64_t mod,uint_fast64_t base> struct RH{
    using u64 = uint_fast64_t;
    size_t n=0;
    vector<u64> hsh,pwr;
    RH(){}
    explicit RH(string& s){
        n=s.length();
        hsh.resize(n+1);
        pwr.resize(n+1);
        pwr[0]=1;
        for(size_t i=0;i<n;++i){
            hsh[i+1]=(hsh[i]*base+s[i])%mod;
            pwr[i+1]=(pwr[i]*base)%mod;
        }
    }
    u64 get(size_t l,size_t r)const{
        u64 a=hsh[r]+mod-(hsh[l]*pwr[r-l])%mod;
        if(a>=mod) a-=mod;
        return a;
    }
    u64 connect(u64 h1,u64 h2,size_t l)const{
        return (h1*pwr[l]+h2)%mod;
    }
    bool match(RH<mod,base>& b,size_t l1){
        if(l1+b.n>n) return false;
        return get(l1,l1+b.n)==b.get(0,b.n);
    }
};
struct RHs{
    using u64 = uint_fast64_t;
    using a3 = array<u64,3>;
    static const u64 mod1=1000000007;
    static const u64 mod2=1000000009;
    static const u64 mod3=998244353;
    static const u64 base1=10009;
    static const u64 base2=10037;
    static const u64 base3=10007;
    size_t n;
    RH<mod1,base1> rh1;
    RH<mod2,base2> rh2;
    RH<mod3,base3> rh3;
    explicit RHs(string& s){
        n=s.length();
        rh1=RH<mod1,base1>(s);
        rh2=RH<mod2,base2>(s);
        rh3=RH<mod3,base3>(s);
    }
    a3 get(size_t l,size_t r)const{
        return a3({rh1.get(l,r),rh2.get(l,r),rh3.get(l,r)});
    }
    a3 connect(a3 h1,a3 h2,size_t l){
        return a3({rh1.connect(h1[0],h2[0],l),rh2.connect(h1[1],h2[1],l),rh3.connect(h1[2],h2[2],l)});
    }
    bool match(RHs& b,size_t l1){
        if(l1+b.n>n) return false;
        return get(l1,l1+b.n)==b.get(0,b.n);
    }
    size_t LCP(RHs& b,size_t l1,size_t r1,size_t l2,size_t r2){
        int len=min(r1-l1,r2-l2);
        int l=-1,r=len+1,m;
        while(r-l>1){
            m=(l+r)/2;
            if(get(l1,l1+m)==b.get(l2,l2+m)) l=m;
            else r=m;
        }
        return (size_t)l;
    }
};
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s,c;
    cin>>s;
    using rh=RH<1000000007,10009>;
    rh rs(s);
    int m,ans=0;
    cin>>m;
    fr(_,m){
        cin>>c;
        rh rc(c);
        fr(i,rs.n) if(rs.match(rc,i)) ++ans;
    }
    cout<<ans<<endl;
}
            
            
            
        