字符串hash问题

来源:互联网 发布:pmp网络课程 编辑:程序博客网 时间:2024/06/06 01:12

一些介绍字符串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 0x7fffffffusing namespace std;#define ULL unsigned long longconst 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;}



0 0