#include"stdio.h"
#include"string.h"

char a[1000010], b[10010] ;
int next[10010] = {-1} ;

void getnext(char p[],int leth)
{
    int i,j=-1;
    for(i=1;i<leth;i++)
    {
        while(j!=-1&&p[i]!=p[j+1])j=next[j];
        if(p[i]==p[j+1])j++;
        next[i]=j;
    }
}

int kmp(char t[],char p[],int leth1,int leth2)
{
    int i,j=-1;
    int count=0;
    getnext(p,leth2);
    for(i=0;i<leth1;i++)
    {
        while(j!=-1&&t[i]!=p[j+1])j=next[j];
        if(t[i]==p[j+1])j++;
        if(j==leth2-1)
            count++;
    }
    return count;
}

int main( )
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",b,a);
        printf("%d\n",kmp(a,b,strlen(a),strlen(b)));
    }
    return 0;
}

 

相关文章:

  • 2021-08-29
  • 2021-10-28
  • 2021-06-21
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-20
  • 2022-01-04
  • 2022-12-23
  • 2022-02-21
  • 2021-12-20
  • 2022-01-28
  • 2022-12-23
相关资源
相似解决方案