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能用在整形数 ...
随机推荐
- 在WPF应用程序中使用Font Awesome图标
Font Awesome 在网站开发中,经常用到.今天介绍如何在WPF应用程序中使用Font Awesome . 如果是自定义的图标字体,使用方法相同. 下载图标字体 在官方网站或github上下载资 ...
- [译]Selenium Python文档:六、页面对象
本章是介绍页面对象设计模式的教程.一个页面对象代表了web应用用户接口的一片区域,你的测试代码将与之交互的. 使用页面对象模式的好处: 可以创建在多个测试样例中都可使用的可重用代码 减少重复性代码 如 ...
- 【Unity优化】如何实现Unity编辑器中的协程
Unity编辑器中何时需要协程 当我们定制Unity编辑器的时候,往往需要启动额外的协程或者线程进行处理.比如当执行一些界面更新的时候,需要大量计算,如果用户在不断修正一个参数,比如从1变化到2,这种 ...
- Linux 下使用网易的SMTP服务器 发送邮件
最近在研究服务器高可用集群 (HA)…… 尝试配置keepalived, 却发现其全局配置(global_defs )中发送邮件的SMTP服务器配置只有简单 smtp_server 一个选项. 那么, ...
- JS的DOM操作及动画
JS的DOM操作DOM:Document Object ModelBOM:Bowers(浏览器) Object Model找到元素:var a=document.getElementById(&quo ...
- WPF之路三:视频的播放
同图片一样,把视频资源复制到文件夹路径下,修改资源属性复制到输出目录"始终复制",生成操作改为“内容”, 把<MediaElement Name="myVid&q ...
- Spring框架下的单元测试
一.使用spring中对Junit框架的整合功能 除了junit4和spring的jar包,还需要spring-test.jar.引入如下依赖: <dependency> <grou ...
- Html5 Canvas笔记(2)-Canvas绘图
用Canvas API绘图,需要画图形的边线并设置内部区域填充,边线英文语法对应stroke,填充对应fill,在后面我们会频繁看到这2个英文单词的出现.Canvas API内置的形状绘图函数比较少, ...
- webpack点滴记录
有了webpack..(不借助gulp/grunt)代码压缩 图片base64,解析less/sass coffee css压缩,MD5加密都帮你做了..还支持按需加载..还有热替换 webpack常 ...
- 手机自动化培训:Appium介绍
手机自动化培训:Appium介绍 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088214 ...