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) ...
随机推荐
- MVC与EasyUI结合增删改查
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的 ...
- (转)C++重写、重载和重定义的区别
C++ 重写重载重定义区别 (源自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/) 1 ...
- JavaScript事件的几个细节
JavaScript事件的几个细节 一.是捕获还是冒泡 昨天被问到一个问题:事件流有几个阶段?在这几个阶段中,事件一共发生几次? 问题很简单,但对于事件一共发生几次有点乱.总觉得捕获也能触发事件.冒泡 ...
- .net下将富文本编辑器文本原样读入word文档
关键词:富文本编辑器 生成word 样式 为了解决标题中提出的问题,首选需要了解,在.net环境下读取数据库中的内容动态生成word至少有2种方式,[方式一]一种方式是在项目中添加引用,例如在“添 ...
- Eclipse plugin web site 发布和版本更新
Eclipse plugin web site 发布和版本更新 在eclipse插件开发过程中免不了要发布1.0, 1.1, 1.2…….等等,随着版本的递增,假如每次都发布一个插件zip包,那使用者 ...
- Arduino 各种模块篇 光敏感应模块 light sensor
It looks like this one: This one isn't a digital light sensor, so it's very simple. http://www.seeed ...
- C#基础之方法和参数
C#基础之方法和参数 接上一篇<C#基础之类型和成员基础以及常量.字段.属性> 实例方法.静态方法 C#中的方法分为两类,一种是属于对象(类型的实例)的,称之为实例方法,另一种是属于类型的 ...
- 通过Jasmine和Guard自动测试JavaScript
原文标题:Autotesting JavaScript with Jasmine and Guard 原文地址:http://edspencer.net/2013/06/15/autotesting- ...
- UVa1003-Cutting sticks
试题描述 将一段木棒按要求切割,每次切割都要付出与木棒长度相同的代价,求最小代价切割. (多组数据) 输入描述 长度L. 切割点数n(n<=50). n个切割点. 输出描述 "The ...
- Html 模态框操作
<style> #modal-overlay { visibility: hidden; position: absolute; /* 使用绝对定位或固定定位 */ left: 0px; ...