博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1711 Number Sequence
阅读量:5800 次
发布时间:2019-06-18

本文共 1985 字,大约阅读时间需要 6 分钟。

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
 
裸KMP算法,也是我第一道KMP算法题
 
#include
#include
#include
#include
#include
#include
using namespace std;#define inf 0xffffff#define maxn 1000005#define maxm 10005int M,N;int S[maxn],T[maxm];void get_next(int T[],int *next){ next[0] = -1; int i = 0,j = -1; while(i < M){ if(j == -1 || T[i] == T[j]){ ++i,++j; if(T[i] != T[j]) next[i] = j; else next[i] = next[j]; } else j = next[j]; }}int KMP(int S[],int T[]){ int i = 0; int j = 0; int next[maxm]; get_next(T,next); while(i < N && j < M) { if(j == -1 || S[i] == T[j]) { j++;i++; } else j = next[j]; } if( j == M ) return i-M+1; else return -1;}int main(){ int t; scanf("%d",&t); while(t--) { cin>>N>>M; for(int i = 0; i < N; i++) scanf("%d",&S[i]); for(int i = 0; i < M; i++) scanf("%d",&T[i]); cout<
<
View Code

我也不想说什么了1个月没有刷题出现缺少分号,主函数打错和在主函数中定义大数组导致程序崩溃我也是醉了,暑假加油!!!!!

转载于:https://www.cnblogs.com/LQBZ/p/4627184.html

你可能感兴趣的文章
iOS_获取网络图片的尺寸Size的最优方法(附:宏定义单例)
查看>>
贴几张我们的婚纱照
查看>>
11gR2游标共享新特性带来的一些问题以及_cursor_features_enabled、_cursor_obsolete_threshold和106001 event...
查看>>
Illustrator国画效果
查看>>
我的友情链接
查看>>
论坛笔记
查看>>
Mysql常见的错误代码
查看>>
UML类图和时序图 易懂
查看>>
python的切片实例可代替for循环
查看>>
我的友情链接
查看>>
php---魔术函数(call())
查看>>
Java中连接各种数据库
查看>>
修改Windows系统IP地址的快捷脚本
查看>>
华为AD+NPS+DHCP+MAC地址认证配置(一)Windows篇
查看>>
Python修改文本列对齐
查看>>
mysql安装
查看>>
java poi Excel文件导出工具类
查看>>
虚拟机VMware NAT service服务手动启动,出错误提示“1067:进程意外终止
查看>>
Drupal 中文社区分布图谱
查看>>
shell编程——if语句 if -z -n -f -eq -ne -lt
查看>>