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<stdio.h>
#include<string.h>
int s1[1000005],s2[10006],next[10006],n,m;
void nextt()
{
int i,j;
i=0;j=-1;
memset(next,-1,sizeof(next));
while(i<m){
if(j==-1 || s2[i]==s2[j]){
i++;j++;next[i]=j;
}
else j=next[j];
}
} int kmp()
{
int i,j;
i=0;j=0;
while(i<n && j<m){
if(j==-1 || s1[i]==s2[j]){
i++;j++;
}
else j=next[j];
}
if(j>=m){
return i+1-m;
}
else return 0;
} int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
scanf("%d",&s1[i]);
}
for(i=0;i<m;i++){
scanf("%d",&s2[i]);
}
nextt();
if(kmp()){
printf("%d\n",kmp());
}
else printf("-1\n");
}
return 0;
}

hdu1711 Number Sequence的更多相关文章

  1. HDU1711 Number Sequence(KMP模板题)

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

  2. HDU1711 Number Sequence KMP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...

  3. hdu1711 Number Sequence kmp应用

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  5. [裸KMP][HDU1711][Number Sequence]

    题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...

  6. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

  7. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

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

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

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. C++获取文件大小常用技巧

    C++编程语言在程序开发应用中能够帮助我们轻松的完成许多功能需求.比如今天为大家介绍的C++获取文件大小的方法,就可以以多种方式轻松的实现.现在将会实现方法呈现给大家,以便大家参考. C++获取文件大 ...

  2. 【从0到1学Web前端】CSS伪类和伪元素

    1.CSS中的伪类 CSS 伪类用于向某些选择器加入特殊的效果. 语法: selector : pseudo-class {property: value} CSS 类也可与伪类搭配使用 select ...

  3. 分类器是如何做检测的?——CascadeClassifier中的detectMultiScale函数解读

    原地址:http://blog.csdn.net/delltdk/article/details/9186875 在进入detectMultiScal函数之前,首先需要对CascadeClassifi ...

  4. [Xcode]使用target进行协同开发

    协同开发时候发现难免会因为某些条件宏导致上传到SVN的代码影响到其他同时,但是每一次去修很多条件编译也不是很方便,所以可以通过新建自己的target来控制product. 一.创建自己的target: ...

  5. 解决NGINX的WORDPRESS伪静态规则失效的问题

    解决NGINX的WORDPRESS伪静态规则失效的问题 前两天搬到了EMSVPS的PR线路上,用上了最新的WDCP2.0管理面板,支持多用户管理(我们几个合租的VPS,最需要这个功能了),感觉很不错, ...

  6. [置顶] 如何在Python IDLE中调试Python代码?

    好久没有用Python了,居然忘记了怎么在Python IDLE中调试Python代码.百度了一下,然后还是写下来吧,以免以后又忘记了. 1. Set break point in the sourc ...

  7. Activity数据传输到服务

    activity数据接口负责启动该服务包.service获取数据.手术. 详细demo如下面: package com.example.android_service_trance; import a ...

  8. Linux中进行挂起(待机)的命令说明

    /*********************************************************************  * Author  : Samson  * Date   ...

  9. Android中的动画具体解释系列【1】——逐帧动画

    逐帧动画事实上非常easy,以下我们来看一个样例: <?xml version="1.0" encoding="utf-8"?> <anima ...

  10. Hello World 之 控制台版本(Console Application)

    原文:Hello World 之 控制台版本(Console Application) 先来介绍下Hello, World   "Hello, World"程序指的是只在计算机屏幕 ...