A - Number Sequence HDU1005 ( kmp 算法+整数数组的应用)

A - 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 <= 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<cstdio>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return 0;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000001;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod  1000000007

int nex[maxn];

void getnext(int*a,int lena)
{
    int i=0,j=-1;
    nex[0]=-1;
    while(i<lena)
    {
        if(j==-1||a[i]==a[j])
            nex[++i]=++j;
        else
            j=nex[j];
    }
}

int kmp(int *a,int lena,int *b,int lenb)
{
    int i=0,j=0;
    while(i<lena&&j<lenb)
    {
        if(j==-1||a[i]==b[j])
            i++,j++;
        else
            j=nex[j];
        if(j==lenb)return i-j+1;
    }
    return -1;
}
int a[maxn],b[maxn];

int main()
{
    //#ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    //freopen("zccccc.txt","w",stdout);
    //#endif // ONLINE_JUDGE
    int n,m,i,j,t,k;
    int T;
    read(T);
    while(T--)
    {
        read_(n,m);
        For(i,0,n)read(a[i]);
        For(i,0,m)read(b[i]);
        getnext(b,m);
        writeln(kmp(a,n,b,m));
    }
    return 0;
}
时间: 2024-10-18 08:33:57

A - Number Sequence HDU1005 ( kmp 算法+整数数组的应用)的相关文章

Number Sequence(kmp算法)

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

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[

Number Sequence(kmp)

欢迎参加——每周六晚的BestCoder(有米!) Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15007    Accepted Submission(s): 6582 Problem Description Given two sequences of numbers : a[1], a[2],

hdoj 1711 Number Sequence 【KMP】

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11817    Accepted Submission(s): 5395 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],

HDU 1711 Number Sequence (简单KMP)

#include <stdio.h> #include <string.h> int next[10005]; int str1[1000005],str2[10005]; void build_next(int len2) { int i=0,j=-1; next[0] = -1; while (i < len2) { if (j==-1 || str2[i] == str2[j]) { i++; j++; if (str2[i] != str2[j]) { next[i]

KMP算法&amp;next数组总结

http://www.cnblogs.com/yjiyjige/p/3263858.html KMP算法应该是每一本<数据结构>书都会讲的,算是知名度最高的算法之一了,但很可惜,我大二那年压根就没看懂过~~~ 之后也在很多地方也都经常看到讲解KMP算法的文章,看久了好像也知道是怎么一回事,但总感觉有些地方自己还是没有完全懂明白.这两天花了点时间总结一下,有点小体会,我希望可以通过我自己的语言来把这个算法的一些细节梳理清楚,也算是考验一下自己有真正理解这个算法. 什么是KMP算法: KMP是三位

HDU 1711 Number Sequence 【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): 42917    Accepted Submission(s): 17715 Problem Description Given two sequences

poj1961(kmp算法next数组应用)

题目链接:https://vjudge.net/problem/POJ-1961 题意:给定一个长为n的字符串(n<=1e6),对于下标i(2<=i<=n),如果子串s(1...i)是周期子串,输出其最大周期. 思路: 考察对kmp算法中next数组的定义掌握,如果(i+1)%(i-j)==0 && (i+1)/(i-j) > 1,那么该子串即为满足条件. AC代码: #include<cstdio> #include<algorithm>

HDU 1711 Number Sequence(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]