CSU 1598: 最长公共前缀(KMP)

CSU 1598: 最长公共前缀 KMP

Description

给定两个字符串s和t,现有一个扫描器,从s的最左边开始向右扫描,每次扫描到一个t就把这一段删除,输出能发现t的个数。

Input

第一行包含一个整数T(T<=50),表示数据组数。
每组数据第一行包含一个字符串s,第二行一个字符串t,字符串长度不超过1000000。

Output

对于每组数据,输出答案。

Sample Input

2
ababab
ab
ababab
ba

Sample Output

3
2

Hint

Source

国防科学技术大学第十八届银河之光文化节ACM程序设计竞赛初赛

思路: KMP模板题

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
//include <map>

using namespace std;
char s1[1000005];
char s2[1000005];
int Next[1000005];
int n,m;
int ans;
int match()
{
    for(int i = 0 , j = 0 ; i < n ;)
    {
        if(s1[i] == s2[j]){
            i++,j++;
            if(j == m){
                ans++;
                j = 0;
            }
        }else if(j == 0) i++;
        else j = Next[j];
    }
    return -1;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%s%s",s1,s2);
        n = strlen(s1);
        m = strlen(s2);
        ans = 0;
        Next[0] = Next[1] = 0;
        for(int i = 1 ; i < m ; i++){
            int k = Next[i];
            while(s2[i] != s2[k] && k != 0)
                k = Next[k];
            if(s2[i] == s2[k])
                Next[i+1] = k + 1;
            else Next[i+1] = 0;
        }
        match();
        cout<<ans<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值