标签:problem 分享 test family mem oid lin soft ios
Problem 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
对于kmp,最重要的是理解next数组( 反映出现失配情况时,应该跳过多少无用字符(也即模式串应该向右滑动多长距离)而进行下一次检测)
模式串的研究是重点,和匹配串无关系,next的下标表示模式串与匹配串匹配到位数,其下标对应的值是该位前模式串max(前缀=后缀)的个数,

这个算出的next数组要减一
刚接触,懵懂(b站视频里面有讲,看了三个人的视频,依旧懵懂,(╥╯^╰╥)(╥╯^╰╥)慢慢来,加油!)
http://billhoo.blog.51cto.com/2337751/411486 这位大佬讲的也不错
#include<iostream>
#include <string.h>
using namespace std;
int t[10005],s[1000005],Next[10005];
void getnext(int t[],int Next[],int n)
{
int i=1;
int j=0;
Next[1]=0;
while(i<n)
{
if(j==0||t[i-1]==t[j-1])
{
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
}
int kmp(int t[],int s[],int Next[],int a,int b)
{
int i=1;
int j=1;
while(i<=a&&j<=b)
{
if(j==0||s[i-1]==t[j-1])
{
i++;
j++;
}
else
j=Next[j];
}
if(j>b)
return i-b;
else
return 0;
}
int main()
{
int n;
int a,b;
cin>>n;
while(n--)
{
cin>>a>>b;
for(int i=0;i<a;i++)
cin>>s[i];
for(int j=0;j<b;j++)
cin>>t[j];
memset(Next,0,sizeof(Next));
getnext(t,Next,b);
int result=kmp(s,t,Next,a,b);
if(result==0)
cout<<"-1"<<endl;
else
cout<<result<<endl;
}
return 0;
}
标签:problem 分享 test family mem oid lin soft ios
原文地址:http://www.cnblogs.com/z-712/p/7291422.html