HDU - 1711 Number Sequence KMP入门

Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one. 

Input

The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000]. 

Output

For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead. 

Sample Input

2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1

Sample Output

6
-1

这是一道KMP模板题,那么什么是KMP?KMP解决什么问题。其实KMP解决的就是字符串匹配问题。我们知道字符串匹配可以很暴力的去找,比方说在字符串s中找t,我们就对着s这个字符串一次次的去找,发现不同了,就两个字符串都重新回头找,就是这样

有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢?

    如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置,则有:

  • 如果当前字符匹配成功(即S[i] == P[j]),则i++,j++,继续匹配下一个字符;
  • 如果失配(即S[i]! = P[j]),令i = i - (j - 1),j = 0。相当于每次匹配失败时,i 回溯,j 被置为0

那么KMP的优化在哪,就是我们不重复的回溯i,i永远一直的向前走,但是对应需要匹配的就不是直接被置为0,而是找到对应的“合适的值”,那么什么是合适的值呢。我们先看一个概念叫固定字符串的最长前缀和最长后缀相同的长度。

举个例子啊:

对于字符串ABCDABD来说

找到了这个东西有什么用?就是在我们发现两个字符不相同的时候。我们不是直接把j置为0,i++,而是我们知道这个字符串的最长前缀和最长后缀相同那么我就知道aba和aba是相同的,已经匹配过,那么i就不用回溯为i++,就直接是匹配失败的那个i++,j也不会直接置为0,而是失配前的子串的最长前缀和最长后缀相同的长度+1.这也就是我们要求的next数组。这里-1表示不存在,0表示存在长度为1,2表示存在长度为3。这是为了和代码相对应。

06

其实我们可以看到我们求nextt数组和KMP的结构形式其实是差不多的,这是为什么呢,其实求nextt的过程就是一次简单“KMP”只是它是和自己比较而已。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;

const int maxn=1000005;
int T,n,m;
int a[maxn],b[maxn],nextt[maxn];

void getnext()
{
    nextt[0]=-1;
    int k=-1;
    for(int i=1;i<m;i++)
    {
        while(k>-1&&b[k+1]!=b[i])
            k=nextt[k];
        if(b[k+1]==b[i])
            k=k+1;
        nextt[i]=k;
    }
}

int KMP()
{
    getnext();
//    for(int i=0;i<m;i++)
//        cout<<nextt[i]<<" ";
//    cout<<endl;
    int k=-1;
    for(int i=0;i<n;i++)
    {
        while(k>-1&&b[k+1]!=a[i])
            k=nextt[k];
        if(b[k+1]==a[i])
            k=k+1;
        if(k==m-1)
            return i-m+1+1;
    }
    return -1;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<m;i++)
            scanf("%d",&b[i]);
        printf("%d\n",KMP());
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值