题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15548    Accepted Submission(s): 6836

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

看了视频也看了博客,对KMP算法算是有了一知半解,先来一发水题吧。

对于next 数组 只需要求模式串,也就是两个串匹配中较小的那个。

【源代码】

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn =1000005;
const int maxb =10005;
int a[maxn],b[maxb];
int nextv[maxb];
void get_next(int b[],int m){ //求模式串的 next数组int i=0;//前缀nextv[0]=-1;int j=-1;//后缀while(i<m){if(j==-1||b[i]==b[j]){i++; j++;if(b[i]==b[j]) //遇到相同元素的优化nextv[i]=nextv[j];elsenextv[i]=j;}elsej=nextv[j];}
}
int KMP(int n,int m){int i=0;int j=0;while(i<n&&j<m){if(j==-1||a[i]==b[j]){i++;j++;}else{j=nextv[j];}}if(j==m){ //只有匹配出结果j才能==m// cout<<"i: "<<i<<" "<<j<<endl;return i-m+1;}else return -1;
}
int main(){int T,n,m;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%d",&a[i]);}for(int i=0;i<m;i++){scanf("%d",&b[i]);}get_next(b,m); //模式串 parternint ans=KMP(n,m);cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/chaiwenjun000/p/5321187.html

HDU 1711 Number Sequence(KMP算法)相关推荐

  1. HDU 1711 -Number Sequence(KMP)

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

  2. HDU 1711 Number Sequence(KMP模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include&l ...

  3. HDU 1711 Number Sequence(算法验证)

    该怎么做.每一个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在奋斗--lx_Zz ...

  4. hdu 1711 Number Sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:在母链中找到子链的位置,输出开始的位置. 1 #include <iostrea ...

  5. HDU 1005 Number Sequence

    [题目]                                                   Number Sequence Time Limit: 2000/1000 MS (Jav ...

  6. HDU.1005 Number Sequence

    原题 HDU.1005 Number Sequence 分类 杂题 题意 给定一个数列{an}\left\{ a_n \right\}{an​}的前两项a1a_1a1​.a2a_2a2​,以及其递推公 ...

  7. Number Sequence (KMP的应用)

    个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会 Given two sequences of numbers : a11, a22, ...... , aNN, ...

  8. HDU - Number Sequence(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  9. 杭电 1711 Number Sequence 1686 2203

    找一串字符中完全匹配的起始位置,利用KMP模版: 代码: View Code 1 #include<iostream> 2 using namespace std; 3 int p[100 ...

最新文章

  1. TCP/IP协议三次握手与四次握手流程解析
  2. 【Java】leetCode 21 合并两个有序链表
  3. 【深度学习】Github上标星1.1W的PyTorch教程,我们给您搬来了
  4. 数据结构之:链表详解
  5. 【Apache POI】Java 读取Excel文件
  6. how is batch operation handled in backend
  7. vs编译c语言停止工作运行库mt,vc++编译时运行库选择(/MT/MTd/MD/MDd)
  8. 新的 Windows Azure 网络安全性白皮书
  9. Nginx笔记总结十一:Nginx重写规则指南
  10. 求助 电脑能正常上网,但开热点手机连上用不了。开的热点显示ipv6无网络访问权限。
  11. android exo解码问题,android – exoplayer-自动更改质量不起作用(hls)
  12. ⚓写写5G网速及页面提速中的延迟加载Lazyloading
  13. 关闭谷歌Chrome浏览器历史记录提示
  14. UE4 | 学习Shader - “赏月、玩灯笼”
  15. android-自定义ImageView-圆形图片绘制代码详解
  16. matlab仿真单关节机器人,用MATLAB玩转机器人--第六章 用MATLAB玩转单关节机器人
  17. 想自由查看自己网站每个页面流量情况?这个工具可以满足需求!
  18. “使用区块链进行安全可信的电子医疗记录共享”外文翻译——2019年4月份
  19. 深度学习_21天实战Caffe.pdf
  20. Spring Cloud Stream Rabbit 3.1.3 入门实践

热门文章

  1. php调用百度地图技术,调用百度地图
  2. php if require,php echo()和print()、require()和include()函数区别说明
  3. python bindings_OpenCV-Python Bindings 如何工作 | 六十四
  4. flowlayout java_【简答题】通过使用flowlayout设计出来 java程序如下图所示
  5. java 合并txt文件_java合并文本文件并删除文件中重复行
  6. java mysql 是否插入 成功_您如何确定使用Java和MySQL插入或更新是否成功?
  7. 笛卡尔积 php,PHP自定义函数生成笛卡尔积
  8. java 输入流可以合并吗_HOW2J Java 文件输入输出流,合并与拆分
  9. 字符串去重、统计不同字符种类数问题
  10. MySQL面试题 | 附答案解析(四)