hdoj 1711 Number Sequence 【KMP】

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11817    Accepted Submission(s): 5395

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模板

代码:

#include <stdio.h>
#include <string.h>
#define M 10005
#define N 1000005

int a[N], b[M], next[M], n, m;

void get_next(){
	int i, j, k;
	i = 1; j = 0;
	while(i<= m){
		if(j==0||b[i] == b[j]){
			++i; ++j; next[i] = j;
		}
		else j = next[j];
	}
}

int kmp(){
	int i, j;
	i = 1, j = 1;
	while(i<=n&&j<= m){
		if(j == 0||a[i] == b[j]){
			++i; ++j;
		}
		else j = next[j];
	}
	if(j > m) return i-j+1;
	return -1;
}

int main(){
	int t;
	scanf("%d", &t);
	while(t --){
		scanf("%d%d", &n, &m);
		memset(next, 0, sizeof(next));
		next[1] = 0;
		int i, j;
		for(i = 1; i <= n; i ++) scanf("%d", &a[i]);
		for(i = 1;  i <= m; ++ i) scanf("%d", &b[i]);
		get_next();
		int ans = kmp();
		printf("%d\n", ans);
	}
	return 0;
} 
时间: 2024-08-05 07:06:24

hdoj 1711 Number Sequence 【KMP】的相关文章

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

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): 42917    Accepted Submission(s): 17715 Problem Description Given two sequences

hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】

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

HDOJ ---1711 Number Sequence

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

Rabin_Karp(hash) HDOJ 1711 Number Sequence

题目传送门 1 /* 2 Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 3 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个模式串的长度的哈希值向右移动一位 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 14:04:26 8 * File

HDU 1711 Number Sequence (简单KMP)

#include <stdio.h> #include <string.h> int next[10005]; int str1[1000005],str2[10005]; void build_next(int len2) { int i=0,j=-1; next[0] = -1; while (i < len2) { if (j==-1 || str2[i] == str2[j]) { i++; j++; if (str2[i] != str2[j]) { next[i]

HDU 1711 Number Sequence(kmp)

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]

HDU 1711 Number Sequence(KMP模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 1000000+5; 6 7 int n,m; 8 9 int next[maxn]; 10 int a[maxn], b[maxn]; 11 12 void get_next() 13 { 1

HDOJ Cyclic Nacklace 3746【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4063    Accepted Submission(s): 1830 Problem Description CC always becomes very depressed at the end of this month, he has checke