义冢OJ P5018 Oulipo

同步:https://buringstraw.win/index.php/archives/35/

义冢OJ P5018 Oulipo

描述

给出两个字符串W和T,求T中有几个W子串

輸入

第一行为数据组数.

每组数据有两行W和T,表示模式串和原始串.

輸出

对每组数据,每行一个数,表示匹配数.

輸入範例 1

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

輸出範例 1

1
3
0

提示

1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W). |W| ≤ |T| ≤ 1,000,000.

KMP板子题

只能写写板子来水水博客了。。。。

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int MAXN=1e6+5;

int nxt[MAXN]={0};

void setNext(string s);
int kmp(string s1,string s2);

int n;

int main()
{
    cin>>n;
    string s1,s2;
    for(int i=1;i<=n;++i)
    {
        cin>>s1>>s2;
        setNext(s1);
        cout<<kmp(s2,s1)<<endl;
    }
    
    return 0;
}
void setNext(string s)
{
    memset(nxt,0,sizeof(nxt));
    nxt[0]=-1;
    int sl=s.size();
    for(int i=1;i<sl;++i)
    {
        int t=nxt[i-1];
        while(s[t+1]!=s[i]&&t>=0)
        {
            t=nxt[t];
        }
        if(s[t+1]==s[i])
        {
            nxt[i]=t+1;
        }
        else
        {
            nxt[i]=-1;
        }
    }
}
int kmp(string s1,string s2)
{
    int i=0,j=0;
    int sl1=s1.size(),sl2=s2.size();
    int cnt=0;
    while(i<sl1)
    {
        if(s1[i]==s2[j])
        {
            ++i;
            ++j;
            if(j==sl2)
            {
                ++cnt;
                j=nxt[j-1]+1;
            }
        }
        else
        {
            if(j==0)++i;
            else j=nxt[j-1]+1;
        }
    }
    return cnt;
}

转载于:https://www.cnblogs.com/buringstraw/p/10553731.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值