Number Sequence

Number Sequence

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 12
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
 

 

Source
HDU 2007-Spring Programming Contest
先输入T,表示有T组测试样例。
然后输入两个数据,表示两个有多长的字符长度,然后匹配两个字符串,返回能够匹配第一点的位置
 
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 int S[1111110],t[200000],Len_S,Len_t;
 5 void getNext(int p[],int next[])
 6 {
 7     int j,k,L;
 8     next[0]=-1;
 9     j=0;
10     k=-1;
11     while(j<Len_t-1)
12     {
13         if(k==-1||p[j]==p[k])
14         {
15             j++;
16             k++;
17             next[j]=k;
18         }
19         else
20             k=next[k];
21     }
22 }
23 
24 int KMPMatch(int s[],int p[])
25 {
26     int next[200000];
27     int i,j;
28     i=0;
29     j=0;
30     getNext(p,next);
31     while(i<Len_S)
32     {
33         if(j==-1||s[i]==p[j])
34         {
35             i++;
36             j++;
37         }
38         else
39         {
40             j=next[j];
41         }
42         if(j==Len_t)
43             return i-Len_t+1;
44     }
45     return -1;
46 }
47 int main()
48 {
49     int T,i,Sign;
50     scanf("%d",&T);
51     while(T--)
52     {
53         scanf("%d%d",&Len_S,&Len_t);
54         for(i=0;i<Len_S;i++)
55             scanf("%d",&S[i]);
56         for(i=0;i<Len_t;i++)
57             scanf("%d",&t[i]);
58         printf("%d\n",KMPMatch(S,t));/*匹配返回长度*/
59     }
60     return 0;
61 }
View Code

 KMP模版(2016.4.19)

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 int Num[2][1100111];
 7 int Len[2];
 8 void getNext(int p[],int next[],int Len_t)
 9 {
10     int i=0,j=-1;
11     next[0]=-1;
12     while(i<Len_t)
13     {
14         if(j==-1||p[i]==p[j])next[++i]=++j;
15         else j=next[j];
16     }
17 }
18 int KMPMatch(int s[],int p[])
19 {
20     int next[200005];
21     int i=0,j=0,sign=0;
22     int Len_t=Len[1],Len_S=Len[0];
23     getNext(p,next,Len_t);
24     while(i<Len_S)
25     {
26         if(j==-1||s[i]==p[j]){i++;j++;}
27         else j=next[j];
28         if(j==Len_t) return i-Len_t+1;
29     }
30     return -1;    /*返回值是子串在主串第一个匹配的起始位置*/
31 }
32 
33 int main()
34 {
35     int T;
36     scanf("%d",&T);
37     while(T--){
38         scanf("%d%d",&Len[0],&Len[1]);
39         for(int i=0;i<Len[0];i++)scanf("%d",&Num[0][i]);
40         for(int i=0;i<Len[1];i++)scanf("%d",&Num[1][i]);
41         printf("%d\n",KMPMatch(Num[0],Num[1]));
42     }
43     return 0;
44 }
View Code

 

posted @ 2014-08-03 17:42  Wurq  阅读(128)  评论(0)    收藏  举报