zoukankan      html  css  js  c++  java
  • [TJOI2013]单词

    字符串hash

    #include<cstdio>
    #include<vector>
    #include<string>
    #include<iostream>
    #include<cstring>
    typedef unsigned long long u64;
    int n,m;
    int ans[210],len[210];
    u64 p[210];
    const int maxn = 1e6 + 1000;
    u64 hsh[maxn],Exp[maxn];
    char buf[maxn];
    int tot;
    char tmp[maxn];
    inline u64 gethash(int l,int r){
        return hsh[r]-hsh[l-1]*Exp[r-l+1];
    }
    int main(){
        std::ios::sync_with_stdio(false),std::cin.tie(0);
        std::cin >> n;
        for(int i=1;i<=n;++i){
            std::cin >> tmp;
            u64 p=0;
            for(int i=0;tmp[i];++i)
                p=p*2333+tmp[i],buf[++tot]=tmp[i];
            buf[++tot]='$';
            len[i]=strlen(tmp);
            ::p[i]=p;
        }
        Exp[0]=1;
        for(int i=1;i<maxn;++i)Exp[i]=Exp[i-1]*2333;
        u64 pp=0;
        for(int i=1;i<=tot;++i)
            hsh[i]=pp=pp*2333+buf[i];
        for(int i=1;i<=tot;++i)
            for(int j=1;j<=n;++j)
                if(i >= len[j] && gethash(i-len[j]+1,i) == p[j])
                    ++ans[j];
        for(int i=1;i<=n;++i)
            std::cout << ans[i] << '
    ';
    }
  • 相关阅读:
    Linux线程(一)
    模板(一)
    C++基础(八)
    C++基础(七)
    C++基础(六)
    C++基础(五)
    2.C#基础(二)
    1.C#基础(一)
    2.给出距离1900年1月1日的天数,求日期
    网络协议破解 SMTP
  • 原文地址:https://www.cnblogs.com/skip1978/p/10350719.html
Copyright © 2011-2022 走看看