HDU1711
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14563 Accepted Submission(s): 6392
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.
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].
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
-1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int s[],t[];
int Next[];
int ans;
void getNext(int b){
int i=,j=-;
Next[]=-;
while(i<b){
if(j==-||t[i]==t[j]){
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
}
int kmp(int a,int b){
int i=,j=,sum=;
while(i<a){
if(j==-||s[i]==t[j]){
i++;
j++;
}
else
j=Next[j];
if(j==b){
ans=i+-b;///该步需要特别注意一下即可
return ans;
}
}
return -;
}
int main(){
int tt;
scanf("%d",&tt);
while(tt--){
memset(s,,sizeof(s));
memset(t,,sizeof(t));
memset(Next,,sizeof(Next));
int t1,t2;
scanf("%d%d",&t1,&t2);
int temp;
for(int i=;i<t1;i++){
scanf("%d",&s[i]);
}
for(int i=;i<t2;i++){
scanf("%d",&t[i]);
}
getNext(t2);
int pd;
pd=kmp(t1,t2);
printf("%d\n",pd); }
return ;
}
HDU1711的更多相关文章
- hdu1711(终于搞懂了KMP算法了。。)
题意:给你两个长度分别为n(1 <= N <= 1000000)和m(1 <= M <= 10000)的序列a[]和b[],求b[]序列在a[]序列中出现的首位置.如果没有请输 ...
- HDU1711 Number Sequence KMP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU1711 题意概括 给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出 ...
- hdu1711 KMP
#include<stdio.h> #include<string.h> #define maxn 1000010 int next[maxn],s[maxn],p[maxn] ...
- KMP的模版实现(以hdu1711为例)
贴代码,觉得带template的有一些大材小用……不过还是按自己风格写吧! /************************************************************* ...
- [裸KMP][HDU1711][Number Sequence]
题意 找到子串在母串出现的第一个位置 解法 裸的KMP 特别的地方 第一次不看模板自己敲的KMP #include<stdio.h> const int maxn=100000; cons ...
- hdu1711 Number Sequence
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU1711:Number Sequence
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- 【HDU1711】Number Sequence
题面 大致题意: 给定两个数列A,B,长度分别为N和M 求出 满足 Ak=B1 ,Ak+1=B2......Ak+M-1=Bm 的最小k值 如果有多个k值输出最小的一个 题解 KMP裸题 直接计算B数 ...
- hdu-1711(hash)
题意:给你T组数据,每组数据分别输入n,m和长度为n的数字数组,和长度为m的数字数组,问你长度为m的数组第一次出现在长度为n的数组的位置 解题思路:标准字符串匹配问题,一般用kmp解,拿来练hash ...
随机推荐
- java通过地址获取主机名
关键代码: try { String str=Chat.getJt().getText().toString();//获取输入内容 String[] ipstr=str.split("[.] ...
- Android音乐播放器的开发实例
本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...
- 通过smtp协议简单实现邮件发送
使用到的类: ①SmtpClient--发送邮件的类(using System.Net.Mail;) ②MailMessage--初始化邮件的类 ③ NetworkCredential--身份验证的类 ...
- js库中$冲突的解决方法
http://www.w3school.com.cn/jquery/core_noconflict.asp
- RHCS
简介 Red Hat Cluster Suite :红帽子集群套件 高可用性.高可靠性.负载均衡.存储共享 高可用集群是 RHCS 的核心功能.当应用程序出现故障,或者系统硬件. 网络出现故障时,应用 ...
- js阻止表单重复提交
//校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...
- POJ1523 SPF
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8254 Accepted: 3772 Description Consi ...
- Python之MySQL
本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一 ...
- poj1787Charlie's Change(多重背包+记录路径+好题)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3720 Accepted: 1125 ...
- Activator.CreateInstance 反射实例化对象
public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...