Number Sequence (HDU 1711)

Number Sequence

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

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<cstdio>
#include<cstring>
using namespace std;
int len1,len2,a[1000005],b[10005],ne[10005];
void getnext()
{
    int i,j;
    ne[0]=0;ne[1]=0;
    for(i=1;i<len2;i++)
    {
        j=ne[i];
        while(j&&b[i]!=b[j])j=ne[j];
        if(b[i]==b[j])ne[i+1]=j+1;
        else ne[i+1]=0;
    }
}

void kmp()
{
    int i,j=0;
    int flag=0;
    for(i=0;i<len1;i++)
    {
        while(j&&a[i]!=b[j])j=ne[j];
        if(a[i]==b[j])j++;
        if(j==len2)
        {
            flag=1;
            printf("%d\n",i-len2+2);
            break;
        }
    }
    if(!flag)cout<<-1;
}

int main()
{
    int T,i;
    cin>>T;
    while(T--)
    {
        cin>>len1>>len2;
        for(i=0;i<len1;i++)cin>>a[i];
        for(i=0;i<len2;i++)cin>>b[i];
        getnext();
        kmp();
    }
    return 0;
}
时间: 2024-08-02 09:49:04

Number Sequence (HDU 1711)的相关文章

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i

uva10706 - Number Sequence(找规律)

题目:uva10706 - Number Sequence(找规律) 题目大意:有这样一串序列11212312341234512345612345671234567812345678912345678910123456789101112345678910...,问第i个位置数的值. 1  2     3       4          5            6              7               ... 解题思路:这题需要发现规律.我一开始还看错题意了.规律是看了别人

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

Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b2, ...,  bt.需要满足两个条件(1)b1≤b2≤…≤bt   (2)b2−b1≤b3−b2≤?≤bt−bt−1.求出最大的 t 为多少. 遗留大半年的题目呀呀呀呀~~~~!!!首先非常感谢乌冬子大半年前的指点迷津,呕心沥血想了大半天举出个反例,以便指出我算法的错误.为了纪念这个伟大的人物(

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

HDU 1711 Number Sequence(KMP模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 1000000+5; 6 7 int n,m; 8 9 int next[maxn]; 10 int a[maxn], b[maxn]; 11 12 void get_next() 13 { 1

HDU 5014 Number Sequence(位运算)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少了一个操作,ans[i] = temp,却没有想到ans[temp] = i;所以就一直卡在这里了,因为我不确定这样是不是一一对应的,好吧,也没有想到这里,就差这么点了. 1 #include<cstdio> 2 #include<cstring> 3 #include<iost

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

2014 网选 5014 Number Sequence(异或)

1 /* 2 题意:a, b两个序列,规定由[0, n]区间的数! 3 求 a[i] ^ b[i] 的和最大! 4 5 思路:如果数字 n的二进制有x位, 那么一定存在一个数字m,使得n^m的所有二进制位 6 都是1,也就是由x位1!这样下去的到的值就是最大值! 7 8 */ 9 #include<iostream> 10 #include<cstring> 11 #include<cstdio> 12 #include<algorithm> 13 #def