Oulipo(哈希公式)

问题描述
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0

2.算法
算法1解题思路
假设有一|S|=5|S|=5的字符串,设SiSi为第ii个字符,其中1≤i≤51≤i≤5。

根据定义分别求出hash[i]hash[i]
hash[1]=s1hash[1]=s1
hash[2]=s1∗p+s2hash[2]=s1∗p+s2
hash[3]=s1∗p2+s2∗p+s3hash[3]=s1∗p2+s2∗p+s3
hash[4]=s1∗p3+s2∗p2+s3∗p+s4hash[4]=s1∗p3+s2∗p2+s3∗p+s4
hash[5]=s1∗p4+s2∗p3+s3∗p2+s4∗p+s5hash[5]=s1∗p4+s2∗p3+s3∗p2+s4∗p+s5
现在我们想求s3s4s3s4的hash值,不难得出为s3∗p+s4s3∗p+s4,并且从上面观察,如果看hash[4]−hash[2]hash[4]−hash[2]并将结果种带有s1,s2s1,s2系数的项全部消掉,就是所求。但是由于pp的阶数,不能直接消掉,所以问题就转化成,将hash[2]hash[2]乘一个关于pp的系数,在做差的时候将多余项消除,从而得到结果。

不难发现,对应项系数只差一个p2p2,而4 - 3 + 1 = 2(待求hash子串下标相减再加一),这样就不难推导出来此例题的求解式子。

hash[4]−hash[2]∗p4−2+1
hash[4]−hash[2]∗p4−2+1
至此,通过对上例的归纳,可以得出如下的公式。

公式
若已知一个|S|=n|S|=n的字符串的hash值,hash[i],1≤i≤nhash[i],1≤i≤n,其子串sl…sr,1≤l≤r≤nsl…sr,1≤l≤r≤n对应的hash值为:

hash=hash[r]−hash[l−1]∗pr−l+1
hash=hash[r]−hash[l−1]∗pr−l+1
考虑到hash[i]hash[i]每次对pp取模,进一步得到下面的式子:

hash=(hash[r]−hash[l−1]∗pr−l+1)%MOD
hash=(hash[r]−hash[l−1]∗pr−l+1)%MOD
看起来这个式子人畜无害,但是对于取模运算要谨慎再谨慎,注意到括号里面是减法,即有可能是负数,故做如下的修正:

hash=((hash[r]−hash[l−1]∗pr−l+1)%MOD+MOD)%MOD
hash=((hash[r]−hash[l−1]∗pr−l+1)%MOD+MOD)%MOD
至此得到求子串hash值公式。

值得一提的是,如果需要反复对子串求解hash值,预处理pp的nn次方效果更佳。
来源https://blog.csdn.net/pengwill97/article/details/80879387
算法1.源代码
#include
using namespace std;
typedef unsigned long long ull;
const ull base = 2333;
const ull mod = 1e9+9;
const int N = 1e6+100;
ull hashes[N],p[N];
ull gethashes(int l,int r){
return (hashes[r]%mod-(hashes[l-1]%modp[r-l+1]%mod)%mod+mod)%mod;//得到l到r之间的哈希值
}
char s[N],t[N];
int main(){
int T;
scanf("%d",&T);
while(T–){
scanf("%s %s",t+1,s+1);//字符输入,不需要取地址
int n=strlen(s+1);
int m=strlen(t+1);
p[0]=1;
for(int i=1;i<=n;i++){
hashes[i]=(hashes[i-1]base%mod+s[i]%mod)%mod;//判断每一段的哈希值
p[i]=(p[i-1]%mod
base%mod)%mod;//累乘得到相当于进制的数
}
ull ans=0;
for(int i=1;i<=m;i++) ans=(ans
base%mod+t[i]%mod)%mod;//子字符串的哈希值
int res=0;
for(int i=m;i<=n;i++){
if(gethashes(i-m+1,i)==ans){//当任意一段字符串相同时 计数
res++;
}
}
printf("%d\n",res);
}
return 0;
}

  1. 总结
    知道哈希值的求值公式后直接套入模板

你可能感兴趣的:(Oulipo(哈希公式))