HDU1711-Number Sequence

简介:

Number Sequence
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11038    Accepted Submission(s): 5038


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算法
AC代码:

#include<stdio.h>
#include<string.h>
int a[1000010],b[10010],next[10010];
int n,m;
void getnext(int p[])
{
    int i = 0;
    int j = -1;
    next[0] = -1;
    while(i < m)
    {
        if(j == -1 || p[i] == p[j])
        {
            i++;
            j++;
            if(p[i]!=p[j])
                next[i] = j;
            else
                next[i] = next[j];
        }
        else
            j = next[j];
    }
}
int KMP(int a[],int b[])
{
    int j = 0;
    int i = 0;
    getnext(b);
    while(i < n && j < m)
    {
        if(j == -1 || a[i] == b[j])
        {
            i++;
            j++;
        }
        else
            j = next[j];
    }

    if(j >= m)
        return i - m+1;
    else
        return -1;
}
int main()
{
    int i,j,T;
    scanf("%d",&T);
    while(T--)
    {
       scanf("%d %d",&n,&m);
       memset(a,0,sizeof(a));
       memset(b,0,sizeof(b));
       memset(next,0,sizeof(next));
       for(i=0;i<n;i++)
       scanf("%d",&a[i]);
       for(i=0;i<m;i++)
       scanf("%d",&b[i]);
    
       printf("%d\n",KMP(a,b));
    }
    return 0;
}
相关文章
|
算法
LeetCode 260. Single Number III
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
94 0
LeetCode 260. Single Number III
|
算法
LeetCode 306. Additive Number
累加数是一个字符串,组成它的数字可以形成累加序列。 一个有效的累加序列必须至少包含 3 个数。除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和。 给定一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是累加数。 说明: 累加序列里的数不会以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。
121 0
LeetCode 306. Additive Number
LeetCode之Number Complement
LeetCode之Number Complement
69 0
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
951 1
|
C++
[LeetCode] Additive Number
Af first I read the title as "Addictive Number". Anyway, this problem can be solved elegantly using recursion.
921 0
[LeetCode] Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wi
1045 0
[LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without
1053 0
|
机器学习/深度学习 人工智能 Java