Number Sequence

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

Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b (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. 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. 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
 

Recommend
lcy
 

没事水一把KMP。。。。。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n,m;

int a[1000010],b[1000010],next[1000010];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(next,0,sizeof(next));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",a+i);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d",b+i);
        }
        for(int i=1;i<m;i++)
        {
            int j=i;
            while(j>0)
            {
                j=next[j];
                if(b==b[j])
                {
                    next[i+1]=j+1;
                    break;
                }
            }
        }
        int pos=-2;
        for(int i=0,j=0;i<n;i++)
        {
            if(j<m&&a==b[j])
            {
                j++;
            }
            else
            {
                while(j>0)
                {
                    j=next[j];
                    if(a==b[j])
                    {
                        j++;
                        break;
                    }
                }
            }
            if(j==m)
            {
                pos=i-m+1;
                break;
            }
        }
        printf("%d\n",pos+1);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 1711 Number Sequence的更多相关文章

  1. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  2. hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  4. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  5. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  6. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  7. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. KMP - HDU 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. 杭电 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. div内容上下居中

    今天无聊闲逛技术群,听一哥们说要在div里面居中span内容. 第一印象:vertical-align: middle; 结果失效.因为他只对属于inline的元素或是inline-block.tab ...

  2. 【转载】Linux启动过程

    转自:http://cizixs.com/2015/01/18/linux-boot-process 简介 我们都知道:操作系统运行的代码是在硬盘上的,最终要跑到内存和 CPU 上,才能被我们使用. ...

  3. 安卓开发30:AsyncTask的用法

    http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43)  在开发Android应用时必须遵守单线程模型的原则: Andro ...

  4. UVA11181Probability|Given(条件概率)

    题目链接 紫书P327 题意:有n个人准备去超市逛,其中第i个人买东西的概率是 Pi .逛完以后你得知有 r 个人买了东西.根据这一信息,计算每个人实际买东西的概率.输入 n ( 1 <= n ...

  5. python redis使用心得

    发布与订阅 连接池代码 redis_conn.py import redis REDIS_CONN = { 'HOST': '192.168.1.11', 'PORT': '6378', 'DB': ...

  6. UrlEncode编码/UrlDecode解码 - 站长工具

    http://tool.chinaz.com/tools/urlencode.aspx

  7. 运用java集合Collections对List进行max和min操作

    我想创建了一个List,里面有一堆的数,一个需求是去掉一个最大值,一个最小值,List有remove的方法,但是找到最大值和最小值的方法不在List里,而是Collections的静态方法.真心觉得还 ...

  8. apache无法正常启动,80端口被占用的解决方法

    apache无法正常启动,80端口被占用的解决方法 网上的方法: 仔细查看提示: make_sock: could not bind to address 0.0.0.0:80 恍然大悟,计算机上安装 ...

  9. easyui tree获取直接子节点而不获取孙子节点方法

    $(node.target.nextElementSibling).children().each(function(index,ele){ if(checked){ $('#rcDimTreeRow ...

  10. DB服务器中的参数优化

    1.swappiness 禁止系统使用swap空间,配置/etc/sysctl.conf中的vm.swappiness=0 2.Scheduler调度 Scheduler调度,指的是磁盘的IO调度算法 ...