HDU1711:Number Sequence

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

//low方法应该是一点一点的找,但是,运用KMP算法会变牛逼。。(KMP竟然还可以用到数组中!!(⊙o⊙)

//这题很有趣,让我知道算法更重要的是思想,不要有局限自己思维

#include <iostream>
#include <cstdio>

using namespace std;
int data1[1000003],data2[1000003];
int nex[1000003],n,m;

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

int kmp()
{
    int i=0,j=0;
    getnext();
    while(i<n)
    {
        if(j==-1||data1[i]==data2[j])
        {
            i++;j++;
        }
        else
        {
            j=nex[j];// j=nex[i]这里又记错了。。。。
        }
        if(j==m)
        return i-m+1;
    }
    return -1;
}

int main()
{
    int l;
    cin>>l;
    while(l--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
        scanf("%d",&data1[i]);
        for(int i=0;i<m;i++)
        scanf("%d",&data2[i]);
        getnext();
        int ans=kmp();
        cout<<ans<<endl;

    }
    return 0;
}
时间: 2024-10-24 10:03:16

HDU1711:Number Sequence的相关文章

1005:Number Sequence(hdu,数学规律题)

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

ACM1005:Number Sequence

Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case cont

hdu5014:number sequence对称思想

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.....n 但顺序与a[]不同 使得 sum(ai ^ bi)最大 注意到2^k =100000(k个0)  2^k-1 =11111(k个1) 那么 (2^k) ^ (2^k-1)=111111(k+1个1)等于 2^(k+1)-1  同样的有 (2^k+1) ^ (2^k-2)=2^(k+1)-1:

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

poj1019(Number Sequence)

题目地址:Number Sequence 题目大意: 有一串序列由阿拉伯数字够成11212312341234512345612345671234567812345678912345678910123456789101112345678910.......然后问你第几位数字是什么输出该数字. 解题思路: 排列组合.假如算第79位数字为多少,先计算出它在那个1-n的区间,可以看出79在1-12这段数字的区间,然后计算出前面1.1-2.1-3....1-11所有数的总和有多少位减去之后,再通过模拟在1

(KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

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

hdu1005 Number Sequence(找循环节)

题目链接: huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力打表,找循环节,但是也不是那么容易发现啊,所以这时候分析一下,因为最后都会mod7,所以总共有7X7总情况,即A 0,1,2,3,4,5,6,7,B也是如此,所以循环节为49,这么这个问题就解决了... 题目: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memo

[AX]AX2012 Number sequence framework :(三)再谈Number sequence

AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal number包含公司代码和财务期间,比如这样的号码J-20-Jan11-000340,J表上Journal,20代表的是公司代码,Jan11为财务期间,000340才是系统生成的流水号. 在创建Numer sequence的界面上可以选择需要的Scope: Company and fiscal cale

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ