HDU 1711 Number Sequence

Source: Internet
Author: User

Tags: KMP

Click here to transfer to HDU 1711

**number Sequence**problem Descriptiongiven BothSequences ofNumbers:a[1],a[2], ...... ,a[N], andb[1], b[2], ..., B[m] (1<= M <=10000,1<= N <=1000000). Your task is toFinda  NumberK which makea[K] = b[1],a[K +1] = b[2], ...... ,a[K + M-1] = B[m]. If there is more than OneK exist, output theSmallest One. Inputthe First  Line  ofInput isa  NumberT which indicate the  Number  ofCases. each Case contains three Lines. The First  LineIs BothNumbers N andJ O1<= M <=10000,1<= N <=1000000). TheSecond  Line containsN integers which indicatea[1],a[2], ...... ,a[N]. TheThird  Line containsM integers which indicate b[1], b[2], ..., b[m]. All integers isinch  theRange of[-1000000,1000000]. Outputfor eachTest Case, you should output One  LineWhich only contain K described above. If No such K exists, output-1instead. Sample Input2 - 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 3 - 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1Sample Output6-1

The main idea: first give you a number T, indicating that there is a T group of data, and then give you two numbers, M and N, respectively, the target string s and the number of matched string p, and then the number of M and N, respectively, to find the first number of the first to match the subscript of success: for example
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3) 1 3
When i = = 6 when the match is successful, if not found on the output-1;

Problem-Solving ideas: KMP algorithm, the key is to find the next array of values, the idea of KMP algorithm is: In the matching process, if a mismatch occurs, if NEXT[J] >= 0, then the target string of the pointer I will not change, the pointer of the pattern string J moved to the Next[j] position to continue to match If next[j]=-1, move I to the right 1 bits and J 0 to continue the comparison.
Here's an example:
P a B a B a

J 0 1 2 3 4

NEXT[J]-1 0 0 1 2
Now on the code:

#include <iostream>#include <cstring>#include <cstdio>using namespace Std;int mN/*int BF (char *s, char *p)//This is a violent do, of course, not advocated, but can refer to (string, hey, I did not change) {int i,j;    i = 0;        while (I < strlen (s)) {j = 0;            while (S[i]==p[j] && J<strlen (p)) {i++;        j + +;        } if (j = = strlen (p)) return I-strlen (p); i = i-j+1; //I backtrack} return-1;} */void Getnext (int *p,int *next)//key is this {intj =0;intK =-1;Next[0] = -1; while(J < N) {if(k==-1||            P[j]==p[k]) {j + +; k++;Next[j] = k; }ElseK =Next[K]; }}intKMP (int *s,int *p){int Next[10010];inti =0;intj =0; Getnext (P,Next); while(I <m)    {if(j==-1||s[I]==p[j]) {i++;        j + +; }Elsej =Next[j];if(j = = N)returnI-n +1; }return-1;}int s[1000000+5],p[1000000+5];intMain () {intT scanf"%d", &t); while(t--) {scanf ("%d%d",&m, &n); for(intI=0; i<m; i++) scanf ("%d",&s[i]); for(intI=0; i<n; i++) scanf ("%d", &p[i]);printf("%d\ n", KMP (s, p)); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 1711 number Sequence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: