hdu 1711---KMP

题目链接

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

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int n[10005];
int s[1000005],a[10005];
int len1,len2;

void kmp()
{
    n[0]=0;
    for(int i=1,k=0;i<len2;i++)
    {
        while(k>0&&a[k]!=a[i]) k=n[k-1];
        if(a[k]==a[i]) k++;
        n[i]=k;
    }
}

int main()
{
    ///cout << "Hello world!" << endl;
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&len1,&len2);
        for(int i=0;i<len1;i++)
            scanf("%d",&s[i]);
        for(int i=0;i<len2;i++)
            scanf("%d",&a[i]);
        kmp();
        int flag=-1;
        for(int i=0,j=0;i<len1;i++)
        {
            while(j>0&&a[j]!=s[i]) j=n[j-1];
            if(a[j]==s[i]) j++;
            if(j==len2) { flag=i-len2+1; break; }
        }
        if(flag>=0) printf("%d\n",flag+1);
        else puts("-1");
    }
    return 0;
}
时间: 2024-10-19 00:27:00

hdu 1711---KMP的相关文章

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 1711 KMP算法模板题

题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串长度". 当发生失配的情况下,j的新值next[j]取决于模式串中T[0 ~ j-1]中前缀和后缀相等部分的长度, 而且next[j]恰好等于这个最大长度. 防止超时.注意一些细节.. 另外:尽量少用strlen.变量记录下来使用比較好,用字符数组而不用string //KMP算法模板题 //hdu

HDU 1711 kmp+离散化

http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30591    Accepted Submission(s): 12870 Problem Description Given two sequences of

Number Sequence HDU 1711 KMP 模板

题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<m

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

hdu 1711 Number Sequence(KMP)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int n,m,next[10010],a[1000010],b[10010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||b[i]==b[j]) i++,j++,next[i]=j; else j=next[

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

KMP算法 hdu 1711 hdu 2203

mark一下,重新温习了 KMP KMP复杂度O(n+m) 这里有一个解释的超级的好的博客,大家可以去看一下:http://blog.csdn.net/v_july_v/article/details/7041827 换言之,对于给定的模式串:ABCDABD,它的最大长度表及next 数组分别如下: 根据最大长度表求出了next 数组后,从而有 失配时,模式串向右移动的位数为:失配字符所在位置 - 失配字符对应的next 值 void GetNext(char* p,int next[]) {

HDU - 1711 A - Number Sequence(kmp

HDU - 1711 A - Number Sequence 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[

HDU - 1711 Number Sequence KMP字符串匹配

HDU - 1711 Number Sequence Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <=