KMP原始标准代码

B - Number Sequence
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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



代码:

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

#define MaxSize 1000005
#define inf 0x3f3f3f3f
#define LL long long int

int n,m;
int ori[MaxSize],tar[MaxSize],NEXT[MaxSize];//有点醉,本来写的next和algorithm库里面的东西重名CE了。所以用不着的头文件别乱写。

void get_NEXT()
{
  int i=0;//i是指在目标串中走到了i位
  int j=NEXT[0]=-1;//把这里设置成-1,而不是0,是非常巧妙的。j一直都是NEXT[i]

  while(i<m)//得到目标串的NEXT,注意范围
    {
      if(j==-1 || tar[j]==tar[i])//j==-1有两种情况,第一个是刚开始从第0位开始的时候,第二个是后面找不到相同的前缀后一位的时候
        NEXT[++i] = ++j;

      else
        j=NEXT[j];

      /*如果一直这样代换下去,走到j=0的时候,然后进行下一次循环:j==-1不满足,
      于是就比较tar[0]和tar[i],就是a.....|b的第一位a和现在在i位上的b比较。
      如果j==0的时候tar[0]==tar[i]还不满足,说明前面所有的前缀的之后一位都没有和当前i位一样的。
      那就应该有NEXT[++i]=0。在代码中,如果j==0的时候tar[0]==tar[i]还不满足,j就会走到-1,
      (因为我们设置的NEXT[0]=-1)。于是下一步循环的时候,++j就就恰是0,于是NEXT[++i]=0,就是巧妙在这里*/
    }
}

int match()
{
  int i=0;//i是指在原始串中走到了i位置
  int j=0;//j是指在目标串中走到了j位置

  while(i<n)//在原始串中从头到尾匹配,如果走到最后,过程中都没有return,说明没有找到匹配。最后while结束后就返回-1
    {
      if(j==-1 || ori[i]==tar[j])//一位一位匹配下去
        {
          i ++;
          j ++;
        }

      else//走到某一位的时候发现不能匹配,那么就移位。移位之后,j就跑到前缀的后一位,然后继续往后匹配。当然此时的i是不应该变的。
        j=NEXT[j];
        /*如果j代换到了j==0,(原始串:....a.....,目标串b.....)那么就是用当前原始串的第i位a比较目标串第一位b
        如果还不能匹配,那么j就代换成-1,,原始串的i就到下一位,目标串的j本来是-1,加1之后就变成0,也就是又从第一位开始比较
        巧妙的=NEXT[0]=-1。*/

      if (j == m) return i-m+1;//匹配完了,j就会走到目标串的最后一位(即m-1位)之后,j++就变成了m,这里就应该return了。
    }

  return -1;
}

int main()
{
  int T;
  scanf("%d",&T);

  while(T--)
    {
      scanf("%d%d",&n,&m);

      for(int i=0; i<n; i++)
        scanf("%d",&ori[i]);

      for(int i=0; i<m; i++)
        scanf("%d",&tar[i]);

      get_NEXT();

      printf("%d\n",match());
    }

  return 0;
}//FROM CJZ


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值