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 解题思路:题目大意:判断第一个串中是否存在第二个串,存在则输出最小的匹配起始位置,不存在则输出-1。
利用fail数组,KMP.
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int N[],M[],fail[];
int n,m;
void KMP(); int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&N[i]);
for(int i=;i<=m;i++)
scanf("%d",&M[i]);
KMP();
}
return ;
} void KMP()
{
memset(fail,,sizeof(fail));
fail[]=-;//初始化fail数组
for(int i=;i<=m;i++)
{
int p=fail[i-];
//如果不满足要求则一直fail直到p=0
while(p>=&&M[p+]!=M[i])
p=fail[p];
fail[i]=p+;
}
int ans=;
for(int i=,p=;i<=n;i++)
{
while(p>=&&M[p+]!=N[i])
p=fail[p];
if(++p==m)
{
ans=i-m+;
p=fail[p];
break;
}
}
if(ans) printf("%d\n",ans);
else printf("-1\n");
}

KMP入门(匹配)的更多相关文章

  1. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  2. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  3. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  5. HDU4300-Clairewd’s message(KMP前缀匹配后缀)

    Clairewd's message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 1686 Oulipo(KMP+计算匹配成功次数)

    一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...

  7. hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)

    首先第一题 戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串? 文明人不要暴力 ...

  8. KMP算法匹配原理以及C++实现

    原创作品,转载请注明出处:点我 假设A表示目标字符串,A="abababaababacb",B表示匹配模式,B="ababacb" 用两个指针i和j分别表示,A ...

  9. HDU2087(KMP入门题)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. JavaScript 要点(十六)RegExp 对象

    RegExp:是正则表达式(regular expression)的简写. RegExp 对象 正则表达式是描述字符模式的对象. 正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模式匹配的强大 ...

  2. JAVA js的escape函数、解析用js encodeURI编码的字符串、utf8转gb2312的函数

    在使用webView时,如果url中参数有中文的话,拦截到的字符串就会类似这样的:http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5 ...

  3. xcode4的环境变量,Build Settings参数,workspace及联编设置

    转自:http://www.cnblogs.com/lancidie/archive/2013/04/08/3007566.html 一.xcode4中的环境变量 $(BUILT_PRODUCTS_D ...

  4. web项目设计中框架的数据流

    这张图虽然简单,但是很好的说明了一个web框架,需要实现那些模块. 图片来自 <Go Web编程>

  5. 关于Excel Networkdays方法的实现

    最近一个程序要求excel输出的日期差为Networkdays. 在网上找了下,没有找到很好的具体实现方法. 要说明的是,微软的Microsoft.Office.Interop.Excel已经实现的N ...

  6. Ewebeditor最新漏洞及漏洞大全

    Ewebeditor最新漏洞及漏洞大全[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  7. Android Studio导入项目

    原文:http://ask.android-studio.org/?/article/21 本篇教程中使用到的Android Studio版本为1.0, Eclipse ADT版本23.0.4.请尝试 ...

  8. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  9. WebView的应用 持续积累

    在我的项目中载入网页时我们会用到WebView这个控件,关于这个控件的相关的比較有用的API在这里记录一下. 第一 webview 设置javascript可用,  mWebView = (WebVi ...

  10. Retina屏下1px border

    layout tltle tags post ios7下移动web开发的几个坑 webapp 1.Retina屏下1px border 由于高清屏的特性,1px是由2×2个像素点来渲染,那么我们样式上 ...