Number Sequence HDU - 1711

Given two sequences of numbers : a11 , a22 , ...... , aNN , and b11 , b22 , ...... , bMM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11 , aK+1K+1 = b22 , ...... , aK+M?1K+M?1 = bMM . If there are more than one K exist, output the smallest one.
InputThe 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 a11

, a22

, ...... , aNN

. The third line contains M integers which indicate b11

, b22

, ...... , bMM

. All integers are in the range of ?1000000,1000000?1000000,1000000

.
OutputFor 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
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 using namespace std;
 6
 7 int nxt[1000005],s[1000005],p[10005];
 8 int T,n,m;
 9
10 void getNext(){
11     nxt[0]=-1;
12     for(int i=0;i<m;i++){
13         int k=nxt[i];
14         while(k!=-1&&p[i]!=p[k]) k=nxt[k];
15         nxt[i+1]=k+1;
16     }
17 }
18
19 int kmp(){
20     getNext();
21     int i=0,j=0;
22     while(i<n&&j<m){
23         while(j!=-1&&s[i]!=p[j]) j=nxt[j];
24         ++i;
25         ++j;
26     }
27     if(j==m) return i-j+1;
28     return -1;
29 }
30
31 int main()
32 {   scanf("%d",&T);
33     while(T--){
34         scanf("%d%d",&n,&m);
35         for(int i=0;i<n;i++) scanf("%d",&s[i]);
36         for(int i=0;i<m;i++) scanf("%d",&p[i]);
37         int ans=kmp();
38         printf("%d\n",ans);
39     }
40     return 0;
41 }
时间: 2024-10-22 09:33:47

Number Sequence HDU - 1711的相关文章

(模板题)Number Sequence -- Hdu -- 1711

http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16080    Accepted Submission(s): 7100 Problem Description Given two sequences of n

Number Sequence HDU 1711 KMP 模板

题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<m

Number Sequence - HDU 1711(KMP模板题)

题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ============================================================================================== #include<stdio.h> #include<string.h> const int MAXM = 1e4+7; const int

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

hdu 1711 Number Sequence(KMP)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int n,m,next[10010],a[1000010],b[10010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||b[i]==b[j]) i++,j++,next[i]=j; else j=next[

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU - 1711 Number Sequence KMP字符串匹配

HDU - 1711 Number Sequence Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <=

HDU 1711 Number Sequence(字符串匹配)

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10571    Accepted Submission(s): 4814 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],