poj 3461 hash——温柔的暴力

Oulipo 
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 46519   Accepted: 18533

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

题意: 给你一个两个只有大写英语字母的字符串,问在一个字符串在另一个字符串出现的次数;

思路:kmp模板题,但是今天我要逆天行道,我就要用hash暴力来做,具体的实现是先把s1的hash值target算出来,然后预处理s2的每一位的hash值hs[i],再暴力比较target是否等于hs[l1+i-1]-pow(base,l1)*hs[i],等则ans+1,感谢这位奆佬的博客让我很快的入门了hash, 点击打开链接 。sh是真的强,如果数据水的话好多kmp,ac自动机,后缀数组的题都能暴力卡过

代码:
 
 
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#define ll long long
#define ull unsigned long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ull hmod1=19260817;
const ull hmod2=19660813;
const double e=exp(1.0);
const double pi=acos(-1);
ull base=133;
ull cbit[maxn],hs[1000005];
char s1[10005],s2[1000005];
ull quick_pow(int n)
{
	ull ans=1,a=base;
	while(n)
	{
		if(n&1)ans*=a;
		n/=2;
		a=a*a;
	}
	return ans;
}
ull check(int l,int r,ull bit)
 {
	return (ull)hs[r]-bit*hs[l-1];
	//	return (ull)hs[r]-(ull)cbit(r-l+1)*hs[l-1];
}
int main()
{
	int t;
	scanf("%d",&t);
	cbit[1]=base;
	/*for(int i=2;i<maxn;i++)
	cbit[i]=cbit[i-1]*base;*/	
	while(t--)
	{
		scanf("%s%s",s1,s2);
		ull target=0;
		int l1=strlen(s1),l2=strlen(s2);
		
		for(int i=0;i<l1;i++)
		target=target*base+(ull)s1[i];
		
		for(int i=0;i<l2;i++)
		hs[i]=hs[i-1]*base+s2[i];
		
		int ans=0;
		ull bit=quick_pow(l1);
		for(int i=0;i+l1<=l2;i++)
	//	if(check(i,i+l1-1)==target)ans++;
		if(check(i,i+l1-1,bit)==target)ans++;
		printf("%d\n",ans);
	}
	return 0;
}

另外附上kmp的代码:
#include<stdio.h>
#include<string.h>
const int maxn=1e6+5;
int n,l,next[maxn];//next 表示 i 位置时的 
char a[maxn],b[10005];
void getnext()
{
	next[0]=-1;
	int k=-1;
	for(int i=1;i<n;i++)
	{
		while(k>-1&&a[k+1]!=a[i])
			k=next[k];
			
		if(a[k+1]==a[i])k++;
		
		next[i]=k;
	}
}
int kmp()
{
	int k=-1,ans=0;
	for(int i=0;i<n;i++)
	{
		while(k>-1&&b[k+1]!=a[i])
		k=next[k];
		if(b[k+1]==a[i])k++;
		if(k==l-1){
		ans++;
		k=next[k];
		}
	}
	return ans;
}
int main()
{
	/*while(scanf("%d",&n)!=EOF)
	{
		scanf("%s",a);
		getnext(); 		
	}*/
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s%s",b,a);
		n=strlen(a);
		l=strlen(b);
		getnext();
		printf("%d\n",kmp());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/swust5120166213/article/details/80154272