首页 > 其他 > 详细

字符串哈希之Rabin-Karp,poj1200

时间:2016-04-02 17:34:20      阅读:171      评论:0      收藏:0      [点我收藏+]
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int array[16000000];
int hash[256];
char s[160000000];
#define mem(a) memset(a,0,sizeof(a))
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        cin>>s;
        int cnt=0;int len=strlen(s);
        mem(array);
        mem(hash);
        for(int i=0; i<len; i++)
        {
            if(hash[s[i]]==0)
                hash[s[i]]=cnt++;
        }
        int res=0;
        for(int i=0; i+n<=len; i++)
        {
            int sum=0;
            for(int j=i; j<i+n; j++)
            {
                sum*=m;
                sum+=hash[s[j]];
            }
            if(!array[sum])
            {
                res++;
                array[sum]=1;
            }

        }
        cout<<res<<endl;
    }
    return 0;
}

 

字符串哈希之Rabin-Karp,poj1200

原文:http://www.cnblogs.com/calmwithdream/p/5347725.html

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