Number Sequence (HDU 1711)
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24116 Accepted Submission(s):
10232
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.
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].
only contain K described above. If no such K exists, output -1
instead.
#include<iostream>
#include<cstring>
using namespace std;
int a[],b[],l1,l2;
int Case,ne[];
void get_ne()
{
int j=,k=-;
ne[]=-;
while(j<l2)
{
if(k==-||b[j]==b[k])
ne[++j]=++k;
else k=ne[k];
}
}
void kmp()
{
int i=,j=;
get_ne();
while(i<l1)
{
if(j==-||a[i]==b[j])i++,j++;
else j=ne[j];
if(j==l2) {cout<<i-l2+<<endl;return;}
}
cout<<-<<endl;
return;
}
int main()
{
cin>>Case;
while(Case--)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
cin>>l1>>l2;
for(int i=;i<l1;i++)cin>>a[i];
for(int j=;j<l2;j++)cin>>b[j];
kmp();
}
}
Number Sequence (HDU 1711)的更多相关文章
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
Number Sequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Pro ...
- 1005 Number Sequence(HDU)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 1005:Number Sequence(hdu,数学规律题)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- HDU 1005 Number Sequence(AC代码)
#include <stdio.h> #include <string.h> int main() { int a,b,n; int i; ]={}; f[]=; f[]=; ...
- HDU 5014 Number Sequence(位运算)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...
- HDU 1005 Number Sequence(找规律)
链接:传送门 题意:略 思路:f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7 -> f(n) = (A * f(n-1)%7 + B * f(n-1)%7) ...
随机推荐
- GDB-Darwin-Extentions
- 译文:User-agent的历史
这是一篇译文,译文出处在文章底部贴出.由于技术水平,英语水平,翻译水平有限,请各路大侠多多指正,谢谢,提高你也提高我:) 几个礼拜之前,我谈论了特征检测和浏览器检测.这篇帖子提到了一点点嗅探U ...
- gcc编译器优化给我们带来的麻烦???
gcc编译器优化给我们带来的麻烦??? 今天看到一个很有趣的程序,如下: ? 1 2 3 4 5 6 7 8 9 int main() { const int a = 1; int * ...
- 一键搭键php网站环境的系统
QzzmServer v2.0正式版发布 首先,感谢网友的热情的测评及反馈,现QzzmServer 2.0正式版已发布.有些朋友反馈制作一个服务器专用版本,在下已将此列入计划中,敬请大家耐心等候. Q ...
- LigerUI权限系统之组织结构
先上图,再看代码.组织结构界面 组织结构添加: 组织结构修改: 组织结构删除: 我在做这个页面的时候treegrid 的远程数据加载让我很头痛,从LigerUI官网提供的Demo来看,它是根据json ...
- 在MVC3中使用WebForm
Mvc和WebForm一直是有争议的两个平台,园子里也有很多人写过这方面的文章,给我印象比较深的是去年的时候看过的两篇文章http://www.cnblogs.com/mikelij/archive/ ...
- 简单的理解deflate算法
简单的理解deflate算法 最近做压缩算法. 用到了deflate压缩算法, 找了很多资料, 这篇文章算是讲的比较易懂的, 这篇文章不长,但却浅显易懂, 基本上涵盖了我想要知道的所有要点. 翻译 ...
- Address already in use: JVM_Bind(端口冲突)
1.错误描述 2011-7-20 11:05:18 org.apache.catalina.core.StandardServer await严重: StandardServer.await: cre ...
- 【Zookeeper】源码分析之持久化--FileTxnSnapLog
一.前言 前面分析了FileSnap,接着继续分析FileTxnSnapLog源码,其封装了TxnLog和SnapShot,其在持久化过程中是一个帮助类. 二.FileTxnSnapLog源码分析 2 ...
- 如何在网站中加入markdown
在vue组件中加入markdown,模板使用的是webpack 我是这样做的: 因为是npm引入的,所以markdown是遵循CommonJS规范的,需要在webpack.base.conf.js里引 ...