HDU 1711 KMP算法 + 拓展KMP算法实现

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 36522    Accepted Submission(s): 15111


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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1358  3336  1686  3746  1251 
题目是裸的KMP 

这里用拓展KMP实现 时间是1439

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_N = 1000024;
int str[MAX_N],T[MAX_N];
int Next[MAX_N],ex[MAX_N];
int n,m;
void getnext(int *str){
    int i=0,j,po;
    Next[0] = n;
    while(str[i]==str[i+1]&&i+1<n)
        i++;
    Next[1] = i;
    po = 1;
    for(i = 2;i<n;i++){
        if(Next[i-po]+i<Next[po]+po)
            Next[i] = Next[i-po];
        else {
            j=Next[po]+po-i;
            if(j<0) j=0;
            while(i+j<n&&str[j]==str[j+i])
            j++;
            Next[i] = j;
            po = i;
        }
    }
}
void exkmp(int *s1,int *s2){
    int i=0,j,po;
    getnext(s2);
    while(s1[i]==s2[i]&&i<m&&i<n)
    i++;
    ex[0] = i;
    po = 0;
    for(i = 1;i<n;++i){
        if(Next[i-po]+i<ex[po]+po)
        ex[i] = Next[i-po];
        else {
            j=ex[po]+po-i;
            if(j<0) j = 0;
            while(i+j<n&&j<m&&s1[j+i]==s2[j])
            j++;
            ex[i] = j;
            po = i;
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(str,0,sizeof(str));
        memset(T,0,sizeof(T));
        int flag = 0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i){
            scanf("%d",&str[i]);
        }
        for(int i=0;i<m;++i){
            scanf("%d",&T[i]);
        }
       // printf("%s\n",str);
        //printf("%s\n",T);
        exkmp(str,T);
        for(int i=0;i<n;i++){
            if(ex[i]==m) {
                printf("%d\n",i+1);
                flag = 1;
                break;
            }
        }
        if(!flag) printf("-1\n");
    }
    return 0;
}

这里用KMP实现 时间为1248ms

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
const int MAX_N = 1e6+5;
int Next[MAX_N];
int str[MAX_N],mo[MAX_N];
void getnext(){
    int i =0,j=-1;
    while(i<m){
        if(j==-1||mo[i]==mo[j]){
            ++i;
            ++j;
            Next[i] = j;
        }
        else j=Next[j];
    }
    return;
}
int kmp(){
    int i=0,j=0;
    while(i<n){
        if(j==-1||str[i]==mo[j]) i++,j++;
        else j= Next[j];
        if(j==m) return i-m+1;
    }
    return -1;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(str,0,sizeof(str));
        memset(mo,0,sizeof(mo));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
            scanf("%d",&str[i]);
        for(int i = 0;i<m;i++)
            scanf("%d",&mo[i]);
        Next[0] = -1;
        getnext();
        if(kmp()==-1) printf("-1\n");
        else printf("%d\n",kmp());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/80805365
今日推荐