HDU - 1711 A - Number Sequence(kmp
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 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].
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
题目是说给a和b数组,求b数组是否为a的连续子数组并且输出匹配的最小位置,如果找不到的话输出-1
直接暴力会超时吧(或许,读清题意后会知道这就是kmp的裸题
一开始因为用的cin交的g++超时了,然后改成scanf和c++标准过了
没啥坑,入门题,拉的满为姐姐的板子,手动模拟了一下next数组,知道了维护的东西大概明白思路了,就过了嘿嘿
然后说一下暴力做法和kmp做法,如果这道题用暴力写,也就是b数组和a数组逐个匹配,最差情况下是每次都比较到最后一个字符发现不对,知道比到最后,复杂度是O(m*n)
你已经是个成熟的acmer了,不能什么题都暴力模拟了。
kmp的写法是对数组b维护一个next数组,拿第一个样例来说,b数组是1 2 3 1 3,当匹配完第四个字符之后发现第五个不一致,但是这个时候又从头匹配的话有些浪费,然后我们发现第一个字符和第四个字符是一样的,所以可以直接跳到原第四个字符对应的位置后进行比较
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3 没有next数组时候,下一步的操作是
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
有的话是
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
#include<stdio.h>
///kmp算法是在线性时间内利用next数组找到tt字符串中是否出现过tt字符串
///时间复杂度O(s+t)
const int N = 1e7+;
const int M = 1e4+;
int nt[N],a[N],b[M],m,n;
int kmp(int *ss,int *tt)///要匹配的是ss
{
int s=n;
int t=m;
if(s>t)return ;
nt[]=-;
int num=;
for(int i=,j=-;i<s;){
if(j==-||ss[i]==ss[j]){
i++;j++;nt[i]=j;
}
else j=nt[j];
}//next数组
for(int i=,j=;i<t;){
if(j==-||ss[j]==tt[i]){
i++;j++;
}
else j=nt[j];
if(j==s){//该处表示在tt字符串中找到了ss字符串(当然你也可以在这个地方记录其他信息,我的是找到就返回1)
return i+-n;
}
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&n);
for(int i = ;i < m;++i)scanf("%d",&a[i]);
for(int i = ;i < n;++i)scanf("%d",&b[i]);
int ans = kmp(b,a);
if(ans)printf("%d\n",ans);
else puts("-1");
}
}
HDU - 1711 A - Number Sequence(kmp的更多相关文章
- HDU 1711:Number Sequence(KMP)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711:Number Sequence(KMP模板,求位置)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- 1711 Number Sequence(kmp)
Number Sequence Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) To ...
- Number Sequence(kmp)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU1711 Number Sequence(KMP模板题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Java web分级测试评分C级感受
上周一进行了java分级测试,但是完成的不太好,先看题目: 石家庄铁道大学选课管理系统 1.项目需求: 本项目所开发的学生选课系统完成学校对学生的选课信息的统计与管理,减少数据漏掉的情况,同时也节约人 ...
- JavaWeb-SpringSecurity在数据库中查询登陆用户
系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门 JavaWeb-Sp ...
- FRP
使用 FRP 反向代理实现 Windows 远程连接 互联网普及率的日渐攀升与 IPv4 资源的持续减少,现在大部分家庭宽带都不会分配公网 IP ,这使一些网络应用的实现多了些困难,像个人的 NAS ...
- 【译】OkHttp3 拦截器(Interceptor)
一,OkHttp 拦截器介绍(译自官方文档) 官方文档:https://github.com/square/okhttp/wiki/Interceptors 拦截器是 OkHttp 提供的对 Http ...
- git一键push至github脚本
######################################################################### # File Name: push.sh # Aut ...
- PHP 封装POD 类
使用POD的过程 //1.造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;host=localhost"; // ...
- [windows菜鸟]Windows API函数大全(完整)
Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一 ...
- 【例3】设有关系模式R(A, B, C, D, E)与它的函数依赖集F={A→BC, CD→E, B→D, E→A},求R的所有候选键。 解题思路:
通过分析F发现,其所有的属性A.B.C.D.E都是LR类属性,没有L类.R类.N类属性. 因此,先从这些属性中依次取出一个属性,分别求它们的闭包:=ABCDE,=BD,=C,=D, =ABCDE.由于 ...
- C++异常实现机制
1.C函数的调用和返回 要理解C++异常机制实现之前,首先要了解一个函数的调用和返回机制,这里面就要涉及到ESP和EBP寄存器.我们先看一下函数调用和返回的流程. 下面是按调用约定__stdcall ...
- linux配置信息收集
CPU型号:# cat /proc/cpuinfo |grep "model name"# cat /proc/cpuinfo | grep name | cut -f2 -d: ...