Description
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
给出两个串S1,S2(只有大写字母),求S1在S2中出现了几次。输入T组数据,对每组数据输出结果。
字符串HASH,将字符串转化为可以用变量表示的数据,主要用于解决字符串匹配问题。
选取两个合适的互质常数b和h(b<h),假设字符串,那么我们定义哈希函数:
。相当于把字符串看作是b进制数。
要改善时间复杂度,还要用到一个滚动哈希的优化技巧。字符串从位置k+1开始长度为m的字符串子串
的哈希值,就可以利用从位置k开始的字符串子串
的哈希值,直接进行如下计算:
。
于是只要这样计算开始位置右移一位后的字符串子串的哈希值,就可以在时间内得到所有位置对应的哈希值,从而可以在
时间内完成字符串匹配。
不同的字符串哈希值相等的概率是很低的,可以当作这种情况不会发生。
可以利用64位无符号整数计算哈希值,取h为2^64,通过自然溢出省去求模运算。
若想万无一失,可以使用双hash,即取两个mod值(一般是1e9+7和1e9+9,这一对是孪生素数),当两个哈希值相等才判断字符串相等。
AC代码:
//CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const ull B=1e9+7;
int contain(string a,string b)//a在b中出现了多少次
{
int al=a.length(),bl=b.length(),ans=0;
if(al>bl) return 0;
//B的a1次方
ull t=1,ah=0,bh=0;
for(int i=0;i<al;i++) t*=B;
//计算a和b长度为al的前缀对应的哈希值
for(int i=0;i<al;i++) ah=ah*B+a[i]-'A'+1,bh=bh*B+b[i]-'A'+1;
//对b不断右移一位,更新哈希值并判断
for(int i=0;i+al<=bl;i++)
{
if(ah==bh) ans++;
if(i+al<bl) bh=bh*B+b[i+al]-'A'+1-(b[i]-'A'+1)*t;
}
return ans;
}
int main()
{
int T;
cin>>T;
string s1,s2;
while(T--)
{
cin>>s1>>s2;
cout<<contain(s1,s2)<<endl;
}
}