题目

Number Sequence

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

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

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e6 + 10;
int a[MAXN],b[MAXN];
int nxt[MAXN];
int n,m;
void getnxt()
{int i=1;int j=0;nxt[0]=0;while(i<m){if(b[i]==b[j]){nxt[i++]=++j;}else if(!j){i++;}else{j=nxt[j-1];}}
}
int kmp()
{int i=0;int j=0;while(i<n && j<m){if(a[i]==b[j]){i++;j++;}else if(!j){i++;}else{j=nxt[j-1];}}if(j==m) return i-m+1;else return -1;
}
int main()
{int t,i,k;  scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<m;i++)scanf("%d",&b[i]);getnxt();cout<<kmp()<<endl;}return 0;
}

HDU 1711 -Number Sequence(KMP)相关推荐

  1. HDU 1711 Number Sequence(KMP算法)

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

  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

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

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

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

  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. HDU - Number Sequence(KMP)

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

  8. 杭电 1711 Number Sequence 1686 2203

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

  9. Number Sequence (KMP的应用)

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

最新文章

  1. voliate修饰数组有作用吗_Arrays.asList()真的就把数组转换为了List集合吗?
  2. Mac安装PhoneGap3
  3. .Net程序员安卓学习之路5:使用xutils注入View和事件以及图片的显示
  4. 中文NER涨点神器!基于多元数据的双流Transformer编码模型
  5. linux nmap下载教程,Linux_在Linux系统上用nmap扫描SSL漏洞的方法,以下载nmap 6.45及以上版本。如 - phpStudy...
  6. JAVA 计算地球上任意两点(经纬度)距离
  7. 设计师应该知道的配色工具,有效提高效率和审美
  8. linux daemon 函数,Daemon 进程的创建
  9. 华为m6升级鸿蒙,华为鸿蒙系统再传喜讯!14款华为旧旗舰喜提新系统:大幅度换血...
  10. 模拟器 安装 apk
  11. 【知识分享】常见的Kepware冗余功能介绍
  12. mysql常用语句之DQL:查询语句
  13. 语音数据增强—使用kaldi对语音加噪
  14. MacOS安装Photoshop2022提示错误代码107(Error Code: 107)(更新)
  15. php导入word文件怎么打开,phpword 导入word
  16. 持久性连接和非持久性连接
  17. linux怎么随机生成数字,详解Linux如何生成随机数字和字符串
  18. java 32位兼容_Java 32位与64位兼容性
  19. 【转载】nuke命令行渲染方法-多线程渲染!
  20. 2D转3D技术的优势

热门文章

  1. Crawler之Scrapy:Python实现scrapy框架爬虫两个网址下载网页内容信息
  2. DL之LSTM:LSTM算法论文简介(原理、关键步骤、RNN/LSTM/GRU比较、单层和多层的LSTM)、案例应用之详细攻略
  3. UOJ #310 黎明前的巧克力 (FWT)
  4. 【题解】hdu 3586 Information Disturbing 二分 树形dp
  5. 十进制转换成二进制列表
  6. Codeforces 766E
  7. linux下oracle数据库服务和监听的启动停止
  8. 阅读《Oracle内核技术揭秘》的读书笔记
  9. 见鬼了,VS2005发布站点不会把Global.asax复上。
  10. nRF5_SDK_12.3.0 编译micro-ecc摘要