hdu1711kmp
InputThe 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 a11, a22, ...... , aNN. The third line contains M integers which indicate b11, b22, ...... , bMM. All integers are in the range of −1000000,1000000−1000000,1000000.
OutputFor 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就行(经典的看毛片算法)参考的这位大牛的博客,讲的很清楚http://blog.csdn.net/starstar1992/article/details/54913261
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int ext[N],str[maxn],ptr[N];
void getnext(int slen)
{
ext[]=-;
int k=-;
for(int i=;i<=slen-;i++)
{
while(k>-&&str[k+]!=str[i])k=ext[k];
if(str[k+]==str[i])k++;
ext[i]=k;
}
}
int kmp(int plen,int slen)
{
int k=-;
for(int i=;i<=plen-;i++)
{
while(k>-&&str[k+]!=ptr[i])k=ext[k];
if(str[k+]==ptr[i])k++;
if(k==slen-)return i-slen+;
}
return -;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t,n,m;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=;i<n;i++)cin>>ptr[i];
for(int i=;i<m;i++)cin>>str[i];
getnext(m);
cout<<kmp(n,m)<<endl;
}
return ;
}
hdu1711kmp的更多相关文章
- hdu--1711--kmp应用在整形数组--Number Sequence
/* Name: hdu--1711--Number Sequence Author: shen_渊 Date: 16/04/17 19:58 Description: 第一次知道,KMP能用在整形数 ...
随机推荐
- android 透明状态栏方法及其适配键盘上推(二)
在上一篇文章中介绍了一种设置透明状态栏及其适配键盘上推得方法.但是上一篇介绍的方法中有个缺点,就是不能消除掉statusbar的阴影.很多手机如(三星,Nexus都带有阴影).即使我用了: <a ...
- (28)初识struts框架
案例剖析中介绍了一个自己写的mystruts框架,这里开始简单的认识struts框架. 1.先看一个简单的struts开发的例子来熟悉一下流程 Struts就是基于mvc模式的框架! (struts其 ...
- WPF中MeasureOverride ArrangeOverride 的理解
1. Measure Arrange这两个方法是UIElement的方法 MeasureOverride ArrangeOverride这两个方法是FrameworkElement的方法,Framew ...
- HTML 表单常用的代码元素
表单: 将数据通过浏览器提交到服务器的媒介.<form action="" method="get/post" ></form> get ...
- iOS开发之数据存储之Preference(偏好设置)
1.概述 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能. 每个应用都有个NSUserDefaults实例,通过它来存取偏好 ...
- MySql Table错误:is marked as crashed and last (automatic?) 和 Error: Table "mysql"."innodb_table_stats" not found
一.mysql 执行select 的时候报Table错误:is marked as crashed and last (automatic?) 解决方法如下: 找到mysql的安装目录的bin/myi ...
- 《HelloGitHub》第12期
<HelloGitHub>第12期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 简介 最开始我只是想把自己在浏览 GitHub 过程中,发现的有意思.高质量 ...
- SEQ序号与ACK序号理解总结
备查 SEQ序号与ACK序号理解总结
- 老李分享:Android -自动化埋点 2
除了上述的事件,Android提供了一个OnTouchListener的监听器,当事件传递到控件的时候,如果控件注册了这个监听器,则会执行监听器中的onTouch方法.同时,如果它返回true,则事件 ...
- Android 学习笔记之实时保存数据-现场保护onSaveInstanceState()
数据保存:在软件开发中我们希望保存下各个Activity数据,以实现客户数据的时时保存,达到较好的用户体验. 那么我们需要解决如下问题: 1.什么时候保存? 2.保存哪些数据? 我想保存应用产 ...