NOIP2016A组模拟7.13】字符串匹配

Description
这里写图片描述

1<=n<=1012

The solution

不要被数据吓着了,其实只要复制后,在进行几次kmp就好了。

如果不明白就看code吧。

Code

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define fd(i,a,b) for (int i=a;i>=b;i--)
#define N 200005
using namespace std;
char s[N],t[N];
int p[N],lqy=0,tqy=0,j=1,len1,len2,nn;
long long ans=0,s1=0,s2=0,n;
void kmp()
{
    j=0;
    memset(p,0,sizeof(p));
    fo(i,2,len1)
    {
        while (j && s[i]!=s[j+1]) j=p[j];
        if (s[i]==s[j+1]) j++;
        p[i]=j;
    }
    j=0;
    fo(i,1,len2)
    {
        while (j && t[i]!=s[j+1]) j=p[j];
        if (t[i]==s[j+1]) j++;
        if (j==len1) ans++,j=p[j];
    }
    j=0;
    fo(i,len2-len1+2,len2+len1-1)
    {
        while (j && t[i]!=s[j+1]) j=p[j];
        if (t[i]==s[j+1]) j++;
        if (j==len1) 
        {
            j=p[j];
            if (n-1) s1++;
        }
    }
}
int main()
{
    //freopen("data.in","r",stdin);
    scanf("%lld",&n);scanf("%s",s+1);scanf("%s",t+1);
    len1=strlen(s+1),len2=strlen(t+1);
    if (j>n) {printf("0\n");return 0;}
    n/=j;
    len2*=j;
    fo(i,len2+1,2*len2) t[i]=t[i-len2];
    kmp();
    printf("%lld\n",ans*n+s1*(n-1));
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值