题目链接: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. 初试WebSocket构建聊天程序

    上一篇文章中使用了Ajax long polling实现了一个简单的聊天程序,对于web实时通信,今天就来试用一下基于WebSocket的长连接方式. WebSocket简介 为了增强web通信的功能 ...

  2. 绑定方式开始服务&调用服务的方法

    1.编写activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  3. [XPath] XPath 与 lxml (五)XPath 实例

    本文继续沿用第三章的 XML 示例文档. 选取价格高于30的 price 节点 # 从父节点进行筛选 >>> root.xpath('//book[price>30]/pric ...

  4. MD5加密与base64编码

    转自:http://blog.csdn.net/sxzlc/article/details/74127268 import java.io.UnsupportedEncodingException; ...

  5. python下安装Scikit-learn

    安装SK-Learn需要依赖的Python安装包有: Python (>= 2.6), NumPy (>= 1.3), SciPy (>= 0.7), 下载python的各种包的地址 ...

  6. cxGrid数据录入

    一.数据录入 1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行   在TcxGridDBTableV ...

  7. 学了Python可以做什么工作

    学了Python可以做什么工作 用 Python 写爬虫 据我所知很多初学 Python 的人都是使用它编写爬虫程序.小到抓取一个小黄图网站,大到一个互联网公司的商业应用.通过 Python 入门爬虫 ...

  8. 【基础】java类的各种成员初始化顺序

    父子类继承时的静态代码块,普通代码块,静态方法,构造方法,等先后顺序 前言: 普通代码块:在方法或语句中出现的{}就称为普通代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出 ...

  9. GOOGLE CODE ANDROID 开源项目 集合

    转:http://blog.csdn.net/dellheng/article/details/7163333 1.        ZXing  http://code.google.com/p/zx ...

  10. sencha touch 视图(view) activate与deactivate事件探讨

    在sencha touch2.2中采用card布局 之前的需求是考虑show,hide事件发现不可取 http://www.cnblogs.com/mlzs/archive/2013/06/13/31 ...