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 ...
随机推荐
- scikit-learn——快速入门
scikit-learn——快速入门 sklearn 快速入门 环境: ubuntu 12.04, 64 bits python 2.7 sklearn 0.14 好几个月没有发博客了,平时的笔记都随 ...
- jQuery使用之(一)标记元素属性
jQuery使用主要介绍jQuery如何控制页面,包含元素的属性.css样式风格.DOM模型.表单元素和事件处理等. 标记元素的属性 html中每一个标记都具有一些属性,他们这个标记在页面中呈现各种状 ...
- IOS 判断日期是今天,昨天还是明天
git地址: https://github.com/JsoonLi/NSDate-Extension - (NSString *)compareDate:(NSDate *)date{ NSTim ...
- CreateCompatibleDC与BitBlt 学习
CreateCompatibleDC与BitBlt CreateCompatibleDC 创建一个与指定设备一致的内存设备描述表. HDC CreateCompatibleDC(HDC hdc //设 ...
- “耐撕”团队 2016.04.08 站立会议
1. 时间 : 15:20--15:40 2. 人员 : Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:http://ww ...
- iOS边练边学--NSURLConnection发送HTTP请求以及NSString和NSData的相互转换
HTTP请求的常见方法 GET 所有参数拼接在URL后面,并且参数之间用&隔开 比如http://520it.com?name=123&pwd=345 传递了2个参数给服务器 name ...
- grunt安装_
grunt_构建WEBJS程序框架,. package.json是文件配置 ====〉〉〉〉 在Gruntfile.js里面会引用到. //目录下直接放node的东西 ,比如: node_module ...
- 17.(转) Android之四大基本组件介绍与生命周期
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...
- ELF(Executable and Linkable Format)
目录 . 引言 . ELF文件格式 . ELF格式分析工具 0. 引言 0x1: ELF文件类型 ELF文件标准里把系统中采用ELF格式的文件归为以下几类 . 可重定位文件(Relocatable F ...
- poj1733Parity game
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7288 Accepted: 2833 Descr ...