题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

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

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

题目大意:给出两个数字串:文本串和模式串,求模式串在文本串中第一次出现的位置。

解题思路:依然是KMP模板题……

 #include<cstdio>
#include<cstring>
#define MAXpat 10000+5
#define MAXstr 1000000+5
int len1,len2;
int Next[MAXpat],str[MAXstr],pat[MAXpat];
void getNext()
{
int i=, j=-;
Next[]=-;
while(i<len2)
{
if(j == - || pat[i] == pat[j]) Next[++i]=++j;
else j=Next[j];
}
}
int kmp()
{
getNext();
int i=, j=;
while(i<len1)
{
if(j == - || str[i] == pat[j]) i++, j++;
else j=Next[j];
if(j == len2) return i-j+;
}
return -;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&len1,&len2);
for(int i=;i<len1;i++) scanf("%d",&str[i]);
for(int i=;i<len2;i++) scanf("%d",&pat[i]);
printf("%d\n",kmp());
}
}

HDU 1711 - Number Sequence - [KMP模板题]的更多相关文章

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

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

  2. hdu 1711 Number Sequence KMP 基础题

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

  3. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  4. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

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

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

  6. HDU 1711 Number Sequence(数列)

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

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

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

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

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

  9. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

随机推荐

  1. jsoup Cookbook(中文版)-Jsoup解析HTML

    jsoup Cookbook(中文版) 入门 1.      解析和遍历一个html文档 如何解析一个HTML文档: String html = "<html><head& ...

  2. 车牌、手机、身份证、等敏感信息 屏蔽 替换 、中文转unicode编码 函数

    应工作要求,需要对展示的内容进行敏感信息替换.琢磨的一些时间,编写的函数匹配率还是比较高的. 顺便说下思路,使用的是正则匹配替换和字符串替换.函数可以再改进. 先把需要匹配的内容写好相应的正则,然后进 ...

  3. Python3.X如何下载安装urllib2包 ?

    python 3.X版本不需要安装urllib2包,因为urllib和urllib2包集合成在一个包了 那现在问题是: 在python3.x版本中,如何使用:urllib2.urlopen()? 答: ...

  4. GreenPlum数据加载

    1. copy命令 对于数据加载,GreenPlum数据库提供copy工具,copy工具源于PostgreSQL数据库,copy命令支持文件与表之间的数据加载和表对文件的数据卸载.使用copy命令进行 ...

  5. Data Guard启动实时日志应用

    1. REDO数据实时应用 启动实时应用的优势在于,REDO数据不需要等待归档完成,接收到即可被应用,这样执行角色切换时,操作能够执行得更快,因为日志是被即时应用的. 要启动实时应用也简单,前提是St ...

  6. SaltStack Grains 和 Pillar

    Grains: (1) grains 是服务器的一系列粒子信息,也就是服务器的一系列物理,软件环境信息(2) grains 是 minion 启动时收集到的一些系统信息,比如操作系统版本.内核版本.C ...

  7. CentOS6.5下安装Oracle11g

    一.安装前系统准备 1. 修改主机名 #sed -i "s/HOSTNAME=localhost.localdomain/HOSTNAME=oracledb.01/" /etc/s ...

  8. 如何在taro的map循环中使用if条件渲染

    在taro的jsx中,鉴于编译的机制,官方明确的表示了不能在map循环中使用if循环, 但是呢,官方也给出了解决办法,那就是提取变量或者是用三目运算嵌套的方法: 链接奉上:https://github ...

  9. 《转载》Python3安装Scrapy

    运行平台:Windows Python版本:Python3.x IDE:Sublime text3 转载自:http://blog.csdn.net/c406495762/article/detail ...

  10. Nginx/LVS/HAProxy负载均衡软件的优缺点

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术.具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了:如果机器不少,可以用D ...