当前位置: 首页 > news >正文

HDU 1711:Number Sequence ← KMP算法

​【题目来源】
https://acm.hdu.edu.cn/showproblem.php?pid=1711

【题目描述】
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.
译文:给定两个序列:a[1]、a[2]……、a[N],以及 b[1]、b[2]……、b[M](1 <= M <= 10000,1 <= N <= 1000000)。你的任务是找到一个数 K,使得 a[K] = b[1]、a[K + 1] = b[2]、……、a[K + M - 1] = b[M]。如果有多个这样的 K 值存在,则输出其中最小的那个。

【输入格式】
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].
译文:输入的第一行是一个整数 T,它表示测试案例的数量。每个测试案例包含三行。第一行有两个整数 N 和 M(1 <= M <= 10000,1 <= N <= 1000000)。第二行包含 N 个整数,表示数组 a 中的元素 a[1]、a[2]……、a[N]。第三行包含 M 个整数,表示数组 b 中的元素 b[1]、b[2]……、b[M]。所有整数的范围都在 [-1000000, 1000000] 之间。

【输出格式】
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
译文:对于每个测试案例,您都应输出一行内容,该内容仅包含上述描述的 K 值。若不存在这样的 K 值,则输出 -1 。

【输入样例】
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

【输出样例】
6
-1

【数据范围】
1<=M<=10000,1<=N<=1000000

【算法分析】
● 这是一道 KMP 算法的裸题。可参考:https://blog.csdn.net/hnjzsyjyj/article/details/160215718
● 题目给数字序列,就用数组 /vector<int>,绝对不要用命令 to_string(x) 转字符串!转字符串 = 必错 + 必超时。

【算法代码】

#include <iostream>
#include <vector>
using namespace std;const int M=1e4+5;
int ne[M];void getNext(vector<int> t) {int len=t.size();int i=0,j=-1;ne[0]=-1;while(i<len) {if(j==-1 || t[i]==t[j]) {i++,j++;ne[i]=j;} else j=ne[j];}
}void KMP(vector<int> s,vector<int> t) {int lens=s.size(),lent=t.size();int i=0,j=0;while(i<lens && j<lent) {if(j==-1 || s[i]==t[j]) {i++,j++;} else j=ne[j];}if(j==lent) cout<<i-j+1<<endl;else cout<<-1<<endl;
}int main() {int T;cin>>T;while(T--) {int n,m,x;cin>>n>>m;vector<int> s,t;for(int i=1; i<=n; i++) {cin>>x;s.push_back(x);}for(int i=1; i<=m; i++) {cin>>x;t.push_back(x);}getNext(t);KMP(s,t);}return 0;
}/*
in:
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 1out:
6
-1
*/





【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/160238859
https://blog.csdn.net/hnjzsyjyj/article/details/160215718
https://blog.csdn.net/hnjzsyjyj/article/details/160234350
https://blog.csdn.net/hnjzsyjyj/article/details/160228242
https://blog.csdn.net/hnjzsyjyj/article/details/160224665

 

 

http://www.jsqmd.com/news/657517/

相关文章:

  • 告别printk:用Linux内核Tracepoint给你的驱动调试换个活法(附ext4实战)
  • 深度解析BlockTheSpot:Spotify桌面端广告拦截的终极解决方案
  • SMPTE SDI核心协议实战解析:从数据包结构到FPGA实现
  • 从网表反推设计:深度拆解XPM_CDC_PULSE宏,看Xilinx如何巧妙解决快慢时钟域脉冲同步难题
  • Airwallex 空中云汇 vs Stripe 2026 最新对比:收费功能合规风控 4 大维度深度评测 - 速递信息
  • 2026年论文降AIGC痕迹:高效规避AI检测的必备指南 - 降AI实验室
  • CANFD飙到10Mbps就出错?别慌,手把手教你搞定收发器延时补偿(以STM32 FDCAN为例)
  • Apollo自定义场景(scenarios)并仿真
  • 革命性桌面分区工具NoFences:智能整理Windows工作空间的终极方案
  • Android APP作为TCP客户端与STM32+ESP8266通信实战:核心代码解析与优化
  • Multisim14仿真进阶:单管共射放大电路参数扫描与性能优化实战
  • 6. 线程
  • 告别瞎摸索!Blender高效建模必装的7个神仙插件及一键配置脚本
  • AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布
  • 保姆级教程:用Matlab R2024b搞定摄像头标定,从生成棋盘格到导出参数一步不落
  • DCS World 任务编辑实战:从零构建你的第一个pydcs自动化任务
  • 别再傻傻分不清了!用Kaggle比赛实例讲透训练集、验证集和测试集到底怎么用
  • DensePose实战部署:从源码编译到避坑指南
  • ST MCSDK V6.2.0实战:手把手教你配置HSO-ST观测器,体验无感电机控制的‘快准稳’
  • 自媒体增长引擎中内容量化成垂直领域知识库的思考
  • 2026年哪家 GEO 平台性价比最高?2026年综合技术、执行、ROI与服务的深度评测与最优选择指南 - 速递信息
  • C# 实战:基于三菱PLC网络通信的两种核心连接方案解析
  • HexView脚本进阶:巧用/FR /FP参数,自动化生成带填充模式的测试固件
  • 捕捉绝对物理真实:DIC系统重构高速振动与疲劳形变的测量秩序
  • Dematel法实战:从关系矩阵到要素权重的系统影响力解码
  • 2026年,中小企业应该怎么选 GEO 平台?2026年预算有限情况下的最优决策与长期品牌建设路线图 - 速递信息
  • 2026上海紧固件专业展看什么?展览规模、展商阵容与采购价值全解析
  • 为什么92%的AI文档项目在SITS2026评审中被否?——从语义合规性到元数据溯源的全链路复盘
  • 从CAN到CANFD:一文搞懂协议差异、电平实测与车载网络升级实战
  • 国民技术 N32G031F8U7 UFQFPN-20 单片机