Number Sequence
Time limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 15068 Accepted Submission (s): 6606
Problem Descriptiongiven-sequences of numbers:a[1], a[2], ..., a[n], and b[1], b[2], ..., b[m] (1 <= M &L t;= 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 is 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 the 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 is in the range of [-1000000, 1000000].
Outputfor Each test case, you should the output one line which only contain K described above. If No such K exists, output-1 instead.
Sample Input213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1
Sample output6-1
Sourcehdu 2007-spring Programming Contest
Recommendlcy | We have carefully selected several similar problems for you:1358 3336 3746 1251 2222 RE: If you can find the P string from the s string, the output p string is in the position of S string;
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 inta[1000005], b[10005];6 intp[1000005];7 intN, M;8 voidGETP ()//to b[] [p[];9 {Ten inti =0, j =-1; OneP[i] =J; A while(I <m) - { - if(j = =-1|| B[i] = =B[j]) the { -i++; J + +; -P[i] =J; - } + Else -j =P[j]; + } A } at intKmp () - { - inti =0, j =0; - while(I <N) - { - if(j = =-1|| A[i] = =B[j]) in { -i++, J + +; to if(J = =m) + returnI-j +1; - } the Else *j =P[j]; $ }Panax Notoginseng return-1; - } the intMain () + { A intT; thescanf"%d", &t); + while(t--) - { $ inti; $scanf"%d%d", &n, &m); - for(i =0; I < n; i++) -scanf"%d", &a[i]); the for(i =0; I < m; i++) -scanf"%d", &b[i]);Wuyi Getp (); theprintf"%d\n", KMP ()); - } Wu return 0; -}
Hangzhou Electric 1711--number Sequence (kmp→→ uses next array to find the position of string in the string)