题目链接: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. 2、一、Introduction(入门):1、Application Fundamentals(应用程序基础)

    一.Introduction(入门) 1.Application Fundamentals(应用程序基础) Android apps are written in the Java programmi ...

  2. 基本select 语句总结

    --------------基本select语句总结 8.6---------------------------------------------------------------------- ...

  3. ios开发之--armv7,armv7s,arm64,i386,x86_64详解

    有时候在运行的时候,经常出现诸如i386的错误,最新一些可能会出现 No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch ...

  4. python读取文件embedded null character是什么原因

    地址的\需要转义符: 将\写成\\ 或者在整个字符串前面添加字母r

  5. c 编译和链接过程

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

  6. /usr/bin/ld: cannot find -lxxx 的解决办法

    /usr/bin/ld: cannot find -lxxx 的解决办法 在软件编译过程中,经常会碰到类似这样的编译错误: /usr/bin/ld: cannot find -lhdf5 这表示找不到 ...

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

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

  8. 【安全开发】Perl安全编码规范

    多年以来,Perl已经成为用于系统管理和WebCGI开发的功能最强的编程语言之一(几乎可以使用Perl做任何功能的程序).但其扩展应用,即作为Internet上CGI的开发工具,使得它经常成为Web服 ...

  9. 解决16bit压缩贴图失真问题

    选择索引模式

  10. Swift - 可选类型详解

    可选类型详解 直接上代码解释 // 类中所有的属性在对象初始化时,必须有初始化值 class Person : NSObject { var name : String? var view : UIV ...