首页 > 其他 > 详细

[TJOI2013]单词

时间:2019-02-03 17:45:30      阅读:192      评论:0      收藏:0      [点我收藏+]

字符串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] << \n;
}

 

[TJOI2013]单词

原文:https://www.cnblogs.com/skip1978/p/10350719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!