哈希Oulipo

题目描述 http://ybt.ssoier.cn:8088/problem_show.php?pid=1455
这个题用到了一种新的解决字符串匹配的方法 哈哈哈哈哈哈哈哈哈希
通过把字符转化为数的方法 比较数也就是比较字符了

没废话了 看代码吧

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ull;//一种数据类型
const ull b=1e9+7;//定义一个极大极讨厌的数
char x[10010],y[1000010];
ull bb[1000005],num[1000005];
int main()
{  int n,i,j;

   cin>>n; bb[0]=1;//注意要将bb[0]设成1 不然都成0l
   for(i=1;i<=1000000;i++) bb[i]=bb[i-1]*b;
   for(i=1;i<=n;i++)
   {
   	  scanf("%s%s",x+1,y+1);//将存储位置后移一个位置
   	  int lx=strlen(x+1),ly=strlen(y+1);
   	  num[0]=0;
   	  for(j=1;j<=ly;j++)
		num[j]=num[j-1]*b+(ull)(y[j]-'A'+1);//转化
	  ull s=0;
	  for(j=1;j<=lx;j++)
	    s=s*b+(ull)(x[j]-'A'+1);//仍在转化
      int t=0;
	  for(j=0;j<=ly-lx;j++)
	    if(s==num[j+lx]-num[j]*bb[lx])//匹配,判断个数
	      t++;
      cout<<t<<endl;    
   } 
  
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42920122/article/details/82952031