HD1686(hash)
传送门
#include<stdio.h>
#include<string.h>
#define N 1000006
typedef unsigned long long ull;
ull p[N], has[N], base = 131;
char t[N], w[N];
void init()
{
p[0] = 1;
for(int i = 1; i< N; i++)
p[i] = p[i-1] * base;
}
ull calw(char s[])
{
int len = strlen(s);
ull sum = 0;
for(int i = len-1; i >=0; i--)
sum = sum * base + s[i];
return sum;
}
void calt(char s[], int len)
{
has[len] = 0;
for(int i = len-1; i >= 0; i--)
has[i] = has[i+1] * base + s[i];
}
int main()
{
int n, len1, len2, sum;
ull tem1;
scanf("%d", &n);
init();
while(n--)
{
sum = 0;
scanf("%s %s", w, t);
len1 = strlen(w);
len2 = strlen(t);
tem1 = calw(w);
calt(t, len2);
for(int i = 0; i <= len2 - len1; i++)
if(tem1 == has[i] - has[i+len1] * p[len1])
sum++;
printf("%d\n", sum);
}
return 0;
}