《HDU 1711 - Number Sequence》的题解
题目的描述
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
题目的大意
又到了翻译理解困难的时间orz,这题的话大概意思是
开头给T组数,然后给出n,m两个数,接下来两行分别输入n个数和m个数。
然后,在第一组数中,找到完全和第二组数一模一样的子串
最后输出那个子串所在的位置的最左边的数所在的位置。
解题思路和方法
这题需要用到神奇的KMP,接触KMP的时候我有点头昏脑涨理解不能hhh
不能暴力解,因为这个数据太大了,一百万呢
用KMP在next数组上标记每个数字上的“前缀”,来方便回溯
主要函数需要用到的代码
void KMP(int *next,int len)
{
int k=-1;
memset(next,0,sizeof(next));//初始化为 0
next[0]=-1; //让next数组的第一个元素初始化为-1
int i;
for(i=1;i<len;i++) //好的开始进入循环
{
while(k>-1&&b[k+1]!=b[i]) //k>-1表明前缀不为0
{
k=next[k]; //当遇到这种情况的时候,进行回溯
}
if(b[k+1]==b[i]) //遇到等于的情况的时候,进行下一步
{
k++; //于是K++
}
next[i]=k; //每次循环结束给next数组赋值
}
}
int Gilgamesh(int lena,int lenb)
{
int *next=new int[lenb]; //创建一个新的数组next
KMP(next,lenb); //把空的数组next和他的长度一起放进KMP里头
int i,k=-1; //好,从KMP回来了以后,next数组上记录的前缀和已经就位啦
for(i=0;i<lena;i++)
{
while(k>-1&&b[k+1]!=a[i])//与KMP函数中一样的条件回溯
{
k=next[k];
}
if(b[k+1]==a[i])
{
k++;
}
if(k==lenb-1) //当k已经等于lenb-1的时候说明b字符串已经在这个位置找完了
{
delete(next); //别忘了归还动态储存空间哦
return (i-lenb+2); //好,于是我们返回了这段数据的开头的位置
}
}
delete(next);
return -1;
}
完整代码
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int a[1000010]={0};
int b[10010]={0};
void KMP(int *next,int len)
{
int k=-1;
memset(next,0,sizeof(next));//初始化为 0
next[0]=-1; //让next数组的第一个元素初始化为-1
int i;
for(i=1;i<len;i++) //好的开始进入循环
{
while(k>-1&&b[k+1]!=b[i]) //k>-1表明前缀不为0
{
k=next[k]; //当遇到这种情况的时候,进行回溯
}
if(b[k+1]==b[i]) //遇到等于的情况的时候,进行下一步
{
k++; //于是K++
}
next[i]=k; //每次循环结束给next数组赋值
}
}
int Gilgamesh(int lena,int lenb)
{
int *next=new int[lenb]; //创建一个新的数组next
KMP(next,lenb); //把空的数组next和他的长度一起放进KMP里头
int i,k=-1; //好,从KMP回来了以后,next数组上记录的前缀和已经就位啦
for(i=0;i<lena;i++)
{
while(k>-1&&b[k+1]!=a[i])//与KMP函数中一样的条件回溯
{
k=next[k];
}
if(b[k+1]==a[i])
{
k++;
}
if(k==lenb-1) //当k已经等于lenb-1的时候说明b字符串已经在这个位置找完了
{
delete(next); //别忘了归还动态储存空间哦
return (i-lenb+2); //好,于是我们返回了这段数据的开头的位置
}
}
delete(next);
return -1;
}
int main()
{
int T,n,m,i,j,works;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(i=0;i<n;i++) scanf("%d",&a[i]); //来让我们读入数据
for(j=0;j<m;j++) scanf("%d",&b[j]); //嗯再读一组,之后是在a里面找b
works=Gilgamesh(n,m); //好,进入函数吉尔伽美什(x)
printf("%d\n",works);//再然后直接输出就可以啦
}
}