Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16796 Accepted Submission(s): 7378
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
/*************************************************************************
> File Name: Number_Sequence.cpp
> Author: Zhanghaoran
> Mail: chilumanxi@xiyoulinux.org
> Created Time: Mon 23 Nov 2015 09:16:28 PM CST
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
int T;
int N, M;
template <class T>
inline bool scan_d(T &ret){
char c;
int sgn;
if(c = getchar(), c == EOF)
return 0;
while(c != '-' && (c < '0' || c > '9'))
c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(int x){
if(x > 9)
out(x / 10);
putchar(x % 10 + '0');
}
/*
* next[]的含义是x[i - next[i]...i - 1] = x[0...next[i - 1]]
* next[i]为满足x[i - z...i - 1] = x[0...z - 1]
*/
void kmp_pre(ll x[], int next[]){
int i, j;
j = next[0] = -1;
i = 0;
while(i < M){
while(j != -1 && x[i] != x[j])
j = next[j];
next[++ i] = ++ j;
}
}
/*
* kmpNext[]的含义是next'[i] = next[next[...[next[i]]]](直到next'[i] < 0或者是x[next'[i] != x[i]])
* 这样的预处理更快些
*/
void pre_KMP(ll x[], int kmpNext[]){
int i, j;
j = kmpNext[0] = -1;
i = 0;
while(i < M){
while(j != -1 && x[i] != x[j])
j = kmpNext[j];
if(x[++ i] == x[++ j])
kmpNext[i] = kmpNext[j];
else
kmpNext[i] = j;
}
}
int nexti[10010];
int KMP_Count(ll x[], ll y[]){
int i, j;
int ans = 0;
//kmp_pre(x, nexti);
pre_KMP(x, nexti);
i = j = 0;
while(i < N){
while(j != -1 && y[i] != x[j])
j = nexti[j];
i ++;
j ++;
if(j >= M){
return i - M + 1;
}
}
return 0;
}
ll a[1000010];
ll b[10010];
int main(void){
cin >> T;
while(T --){
cin >> N >> M;
for(int i = 0; i < N; i ++){
scanf("%lld", &a[i]);
}
for(int j = 0; j < M; j ++){
scanf("%lld", &b[j]);
}
int temp = KMP_Count(b, a);
if(temp){
out(temp);
puts("");
}
else
puts("-1");
}
}
当然如果有兴趣看过这篇文章 全套字符串匹配算法。会发现我还记录了很多的其他的字符串匹配算法,并且看起来比KMP都要快,但是这个题目的特殊之处和KMP引以为傲的所在就是在他不但可以针对字符串,更可以针对数字串,如果对于类似Sunday算法之类的,他们需要将该字符对应的空间存上该字符在字符串中的位置,但是数字实在太多,太浪费空间和时间用来初始化next数组,所以我们对于这种数字类型的匹配只使用KMP算法。