HDU1711(KMP)

Number Sequence

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

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
HDU 2007-Spring Programming Contest

kmp模板题,要匹配的从字符变成了数字,仅此而已。
#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
using namespace std;
inline void BuildNext(int pattern[], int length, int next[])
{
    int i, t;
    i = 1;
    t = 0;
    next[1] = 0;
    while(i < length + 1)
    {
        while(t > 0 && pattern[i - 1] != pattern[t - 1])
        {
            t = next[t];
        }
        ++t;
        ++i;
        if(pattern[i - 1] == pattern[t - 1])
        {
            next[i] = next[t];
        }
        else
        {
            next[i] = t;
        }
    }
    //pattern末尾的结束符控制,用于寻找目标字符串中的所有匹配结果用
    while(t > 0 && pattern[i - 1] != pattern[t - 1])
    {
        t = next[t];
    }
    ++t;
    ++i;
    next[i] = t;
}
int KMP(int text[], int text_length, int pattern[], int  pattern_length,int matches[])
{
    int i, j, n;
    int next[pattern_length + 2];
    BuildNext(pattern, pattern_length, next);
    i = 0;
    j = 1;
    n = 0;
    while(pattern_length + 1 - j <= text_length - i)
    {
        if(text[i] == pattern[j - 1])
        {
            ++i;
            ++j;
            //发现匹配结果,将匹配子串的位置,加入结果
            if(j == pattern_length + 1)
            {
                matches[n++] = i - pattern_length;
                j = next[j];
                return 0;
            }
        }
        else
        {
            j = next[j];
            if(j == 0)
            {
                ++i;
                ++j;
            }
        }
    }
    return n;
}
int text[1000020];
int pattern[10020];
int matches[1000020];
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        for(int i=0;i<=1000019;i++)
            matches[i]=-1;
        for(int i=0;i<a;i++)
            scanf("%d",&text[i]);
        for(int i=0;i<b;i++)
            scanf("%d",&pattern[i]);
        int ans=KMP(text, a, pattern, b,matches);
        if(matches[0]==-1)
            printf("-1\n");
        else
            printf("%d\n",matches[0]+1);
    }
}

你可能感兴趣的:(HDU1711(KMP))