Forgiving Matching

这篇博客主要讨论了一种字符串匹配算法,该算法考虑了字符串的宽恕匹配,即允许一定的不匹配次数。文章详细介绍了如何计算在一个字符串中出现另一个字符串的子串次数,同时处理了包含通配符的情况。算法使用快速傅里叶变换(FFT)进行高效计算,并给出了C++实现代码。
摘要由CSDN通过智能技术生成

       Little Q is now checking whether string A matches B. Two strings are considered matched if they have the same length, and there is no position i that Ai is different from Bi . However, Little Q is a kind man, he forgives every person who hurt him. What’s more, he even forgives strings! He gives the string k opportunities, if there are no more than k positions i that Ai is different from Bi , then Little Q will also consider the two strings matched. Note that both of the strings may contain the wildcard character ‘*’, which can match exactly one any character, in such a case this pair won’t consume the forgiveness opportunities. Let’s denote occ(S, T) as the number of substrings in string S which matches T, two substrings are considered different if they start in different places. You will be given two strings S and T, write a program to compute the value of occ(S, T) for k = 0, 1, 2, . . . , |T|.

Input

The first line contains a single integer K (1 ≤ K ≤ 100), the number of test cases. For each test case: The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 200 000), denoting the length of S and the length of T. The second line contains a string S of length n. The third line contains a string T of length m. It is guaranteed that the sum of all n is at most 1 000 000. Both S and T can only contain the characters in {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘*’}.

Output

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=524305;
const double pi=acos(-1.0);
struct comp{
  double r,i;comp(double _r=0,double _i=0){r=_r,i=_i;}
  comp operator+(const comp&x)const{return comp(r+x.r,i+x.i);}
  comp operator-(const comp&x)const{return comp(r-x.r,i-x.i);}
  comp operator*(const comp&x)const{return comp(r*x.r-i*x.i,r*x.i+i*x.r);}
  comp conj(){return comp(r,-i);}
}w[N],ww[N],A[N],B[N];
int pos[N],Case,n,m,k,i,j,o,s[N],f[N],ans[N];char a[N],b[N];
inline void FFT(comp a[],int n,int o){
  for(int i=1;i<n;i++)if(i<pos[i])swap(a[i],a[pos[i]]);
  for(int d=0,k=__builtin_ctz(n);(1<<d)<n;d++){
    int m=1<<d,m2=m<<1;
    for(int i=0;i<n;i+=m2)for(int j=0;j<m;j++){
      comp&A=a[i+j+m],&B=a[i+j],t=(o==1?w[j<<(k-d)]:ww[j<<(k-d)])*A;
      A=B-t;B=B+t;
    }
  }
  if(o==-1)for(int i=0;i<n;i++)a[i].r/=n;
}
int main(){
  scanf("%d",&Case);
  while(Case--){
    scanf("%d%d%s%s",&n,&m,a,b);
    reverse(b,b+m);
    for(k=1;k<=n+m-2;k<<=1);
    j=__builtin_ctz(k)-1;
    for(i=0;i<k;i++)pos[i]=pos[i>>1]>>1|((i&1)<<j);
    for(i=0;i<k;i++)w[i]=comp(cos(pi*i/k),sin(pi*i/k));
    for(i=0;i<k;i++)ww[i]=w[i],ww[i].i*=-1;
    for(i=0;i<n;i++){
      s[i]=0;
      if(i)s[i]+=s[i-1];
      if(a[i]=='*')s[i]++;
    }
    int cnt=0;
    for(i=0;i<m;i++)if(b[i]=='*')cnt++;
    for(i=m-1;i<n;i++){
      f[i]=cnt+s[i];
      if(i>=m)f[i]-=s[i-m];
    }
    for(o=0;o<=10;o++){
      char target=o+'0';
      if(o==10)target='*';
      for(i=0;i<k;i++)A[i]=comp(0,0);
      for(i=0;i<n;i++)if(a[i]==target)A[i].r=1;
      for(i=0;i<m;i++)if(b[i]==target)A[i].i=1;
      FFT(A,k,1);
      for(i=0;i<k;i++){
        j=(k-i)&(k-1);
        B[i]=(A[i]*A[i]-(A[j]*A[j]).conj())*comp(0,-0.25);
      }
      FFT(B,k,-1);
      for(i=m-1;i<n;i++){
        int tmp=((int)(B[i].r+0.5));
        if(o<10)f[i]+=tmp;else f[i]-=tmp;
      }
    }
    for(i=0;i<=m;i++)ans[i]=0;
    for(i=m-1;i<n;i++)ans[m-f[i]]++;
    for(i=0;i<=m;i++){
      if(i)ans[i]+=ans[i-1];
      printf("%d\n",ans[i]);
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值