码迷,mamicode.com
首页 > 其他好文 > 详细

HDU--4821(字符串哈希)

时间:2015-08-13 22:19:15      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821


用到了BKD字符串哈希的方法,一直不是很会哈希,总是用map,但是很显然,map并不是万能的。

用哈希的方法可以递推的求出所有子串的哈希值,最后用map维护答案即可;注意下long long 




#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<climits>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;

const int maxd=1e5+5;
const int seed=31;
const int INF = 0xffffffff;
///===================
char str[maxd];
llu k[maxd],_hash[maxd];
map<llu,int> s;
int M,L;

void BKD_hash(int len)
{
    k[0]=1;
    for(int i=1; i<=L; i++)
        k[i]=k[i-1]*seed;
    for(int i=len-1; i>=0; i--)
        _hash[i]=_hash[i+1]*seed+(str[i]-'a'+1);
}

int main()
{
    while(scanf("%d%d",&M,&L)==2)
    {
        scanf("%s",str);
        int len=strlen(str),cnt=0;
        BKD_hash(len);
        for(int i=0; i<L && M*L+i<len; i++)
        {
            s.clear();
            for(int j=i; j<M*L+i; j+=L)
            {
                llu id=_hash[j]-_hash[j+L]*k[L];
                s[id]++;
            }
            if(s.size()==M) cnt++;

            for(int j=M*L+i; j<len-L+1; j+=L)
            {
                llu head=j-M*L;
                llu id=_hash[head]-_hash[head+L]*k[L];
                s[id]--;
                if(s[id]==0) s.erase(id);
                llu id2=_hash[j]-_hash[j+L]*k[L];
                s[id2]++;

                if(s.size()==M) cnt++;
            }
        }

        printf("%d\n",cnt);
    }
    return 0;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU--4821(字符串哈希)

标签:

原文地址:http://blog.csdn.net/whoisvip/article/details/47393691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!