HDU-2087 cut fabric strip (the KMP)

The meaning of problems: given two strings, wherein a number of seek occurs in the other (non-matching repeat)

Idea: Since is not repeatable, it is with respect to the POJ-3461 (allowing repeated match), KMP movement to be modified, this time will substring index is set to 0 to restart the matching

 

Complete code:

#include <the iostream> 
#include <cstdio>
 the using  namespace STD;
 const  int MAXN 1E4 + = 10 ;
 int NUM [ 100 * MAXN];
 int num1 [MAXN];
 int NEX [MAXN];
 int N, M;
 int K, LEN1 , LEN2, ANS = - . 1 ;
 // Qiuzi nex string array 
void getnex () {
     int I = 0 , J = - . 1 ; 
    nex [I] = J;
     the while (I < LEN2) {
         IF (J = = - 1||num1[i]==num1[j]){
            i++;j++;
            nex[i]=j;
        }
        else j=nex[j];
    }
}
int KMP(){
    int i = 0, j = 0;
    getnex();
    while(i < N && j < M)
    {
        if(j == -1 || num[i] == num1[j])
        {
            i++; j++;
        }
        else
            j = nex[j];
    }
    if(j == M)
        return i -M + 1;
    else
        return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
    int T;
    cin>>T;
    while(T--){
        
        cin>>N>>M;
        for(int i=0;i<N;i++) cin>>num[i];
        for(int i=0;i<M;i++) cin>>num1[i];
        len1 = N, len2 = M; 
        years = KMP (); 
        cout << age << endl; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11208627.html
cut