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

 

 

题意:给你 两个数组,让你找 a 数组中包含b 数组吗,包含的话,就直接输出b数组的一个数在a数组的中的位置下表,若包含多个的话,输出下标最小的那个;

思路:因为 数组长度的限制,在这我想的kmp算法;

kmp 算法详解:点击打开链接

kmp算法主要就是找 f[]数组,也就是其他文章中的next[]数组,f[i]表示当b数组下表为i时的最长前缀后缀;kmp算法就是运用前面的已知信息

b0....bj-2,bj-1,bj 和bi-j....bi-2,bi-1,bi;当比较bj与bi是否相等时,这个时候已经知道了b0....bj-1和bi-j....bi-1相等了,kmp算法就是运用这个信息,所以当bj与bi不相等时,就是代码中就f数组时,j = f[j],也就是这里的j = f[j-1], 因为已经知道了b0....bj-1和bj-k....bi-1相等了,把j回溯赋值为f[j-1],也就是在他们相等这部分中找最长前缀后缀;不是j回溯到了f[j-1],假设f[j-1]==1,也就是有从b0...bj-1中最长前缀后缀是2(因没有前缀后缀相等时为-1),也就是,b0,b1等于bj-2,bj-1,因为前面已知bj-2,bj-1等于bi-2,bi-1,所以b0,b1等于bi-2,bi-1;   // 保证回溯一定是正确的,因为f[i] 数组中存的就是0~i的最长前缀后缀

求解f[i]时,f[0]~f[i-1]已经都求出了

kmp的本质:

看a串中是不是含有b串,b串匹配a串,kmp其实先求出 b串的f[]数组(也是算是先自身与自身匹配,
求出f[i] ,f[i]中存b串中0~i串的最长前缀后缀)运用前面匹配后的已知信息,当b串与a串匹配时,减少第二层循环的循环次数
也就是第二层循环的起始位下标,但那个a串还是一位一位的加往前进。 

kmp模板 代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 1000010
int n,m;
int a[Max];
int b[10010];
int f[10010]; // f[i] 代表当 b[]数组 在位置i时的最长前缀后缀;

//在求f[i] 之前,从0到i-1 时的f 数组中的数已经求出  
void getF()    // 求b数组的f[],也就是差不多b数组与b数组匹配了 
{
	int i,j;     // 想着写 
	f[0] = -1;   // 先定义f[0]等于-1 
	for(int i = 1;i<m;i++){     //  i 代表这后缀的最后一个字符的位置。
		j = f[i-1];             //  j+1 代表着前缀的最后一个字符的位置。
		while(b[j+1]!=b[i]&&j!=-1)  // 运用前面的所知道最长前缀后缀
			j = f[j];
		if(b[j+1]==b[i])
			f[i] = j + 1;
		else f[i] = -1;
	}
}

int solve()
{
	int j = -1;
	int kk = 0;
	for(int i = 0;i<n;i++){
		
		while(b[j+1]!=a[i]&&j!=-1)
			j = f[j];
		if(b[j+1]==a[i]){
			j++;
			if(j==m-1)
				return i-m+2;
		}
	}
	return -1;
}

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

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值