字符串hash问题

一些介绍字符串hash的文章:

字符串哈希函数



String

题目传送:HDU - 4821 - String

13年长春现场赛的字符串水题。。

不过没有接触过这类题,所以不会做。。

所谓字符串hash,通常是把一个字符串映射为一个整数。

我这里采用的是BKDRHash函数。稳定性最好的一个字符串hash函数。

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;

#define ULL unsigned long long

const int maxn = 100005;

int m, l;

char s[maxn];

ULL base[maxn];//base[i]存i个seed相乘的值
ULL Hash[maxn];//Hash[i]存后缀i的Hash值
ULL seed = 31;//用于映射出去,为了减少冲突,通常取31,131等等这样的质数

map<ULL, int> mp;//用于判重

int main() {
    base[0] = 1;
    for(int i = 1; i <= maxn; i ++) base[i] = base[i - 1] * seed;
    while(scanf("%d %d", &m, &l) != EOF) {
        scanf("%s", s);
        int len = strlen(s);
        Hash[len] = 0;
        for(int i = len - 1; i >= 0; i --) {//字符串hash
            Hash[i] = Hash[i + 1] * seed + s[i] - 'a' + 1; 
        }
        int ans = 0;
        for(int i = 0; i < l && i + m * l <= len; i ++) {
            mp.clear();
            for(int j = i; j < i + m * l; j += l) {
                mp[Hash[j] - Hash[j + l] * base[l]] ++;
            }
            if(mp.size() == m) ans ++;
            for(int j = i + m * l; j + l <= len; j += l) {
                ULL tmp = Hash[j - m * l] - Hash[j - (m - 1) * l] * base[l];
                mp[tmp] --;
                if(mp[tmp] == 0) mp.erase(tmp);
                tmp = Hash[j] - Hash[j + l] * base[l];
                mp[tmp] ++;
                if(mp.size() == m) ans ++; 
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值