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 1867 2203
Problem: KMP template compilation error many times, no love ...
Code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 intp[10010],s[1000010];5 intn,m;6 intnex[10010];7 8 void Get()9 {Ten intplen=m; Onenex[0]=-1; A intk=-1, j=0; - while(J <Plen) { - if(k==-1|| P[J] = =P[k]) { the++J; -++K; - if(P[j]! =P[k]) -nex[j]=K; + Else -nex[j]=Nex[k]; + } A Else{ atk=Nex[k]; - } - } - } - - intKMP () in { - intI=0, j=0; to intslen=N; + intplen=m; - while(I < Slen && j<Plen) { the if(j==-1|| s[i]==P[j]) { *++i; $++J;Panax Notoginseng } - Else{ thej=Nex[j]; + } A } the if(J = =Plen) + returni-j+1; - Else $ return-1; $ } - - intMain () the { - intT;Wuyiscanf"%d",&t); the while(t--){ -scanf"%d%d",&n,&m); Wu for(intI=0; i<n; i++) -scanf"%d",&s[i]); About for(intI=0; i<m; i++) $scanf"%d",&p[i]); - Get(); -printf"%d\n", KMP ()); - } A}
1711 number Sequence (KMP)