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) ...
随机推荐
- 2014.first[未填]
之后就按照自己的直觉,整理了第一套,难度为简单,差不多比2013noipday1水一点...先练练手而已 T1 vijos1196吃糖果游戏 博弈论 依题意,我们可知,如果去分数目为2,3,7,8必输 ...
- python 爬虫总结【转】
1.基本抓取网页 get方法 import urllib2 url = "http://www.baidu.com" response = urllib2.urlopen(url) ...
- 自制AutoMapper实现DTO到持久层Entity的转换
自制AutoMapper实现DTO到持久层Entity的转换 项目中经常涉及到页面DTO更新,保存到数据库的操作,这就必然牵扯到DTO和持久层对象的转换,常见的第三方库有: java:dozer .n ...
- iOS extern使用教程
ios开发使用extern访问全局变量 使用extern关键字法: 1 .新建Constants.h文件(文件名根据需要自己取),用于存放全局变量: 2. 在Constants.h中写入你需要的全局变 ...
- Facebook开源的基于SQL的操作系统检测和监控框架:osquery daemon详解
osqueryd osqueryd(osquery daemon)是可以定期执行SQL查询和记录系统状态改变的驻守程序. osqueryd能够根据配置手机归档查询结果,并产生日志. 同时也可以使用系统 ...
- ios-制作静态.a文件
一.制作静态库文件 /*静态库制作*/ // MakeA.h -(NSString*)testA; // MakeA.m -(NSString*)testA{ return @"静态库测试成 ...
- js Get中文乱码 转码
encodeURI escape post: location.href = "ActivitiesOnSale?CurrId=" + Cid + "&CNam ...
- BZOJ1013 [JSOI2008]球形空间产生器sphere(高斯消元)
1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4846 Solved: 2525[Subm ...
- OpenGL的状态机理解
OpenGL是一种状态机模式,比如你用glEnable打开一个状态,在以后的绘图中将一直保留并应用这个状态,除非你调用glDisable及同类函数来改变该状态或程序退出.例如当前颜色是一个状态变量,可 ...
- 【01背包】HDU 2546 饭卡
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) ...