Problem Description
Given 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.
 
Input
The 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].
 
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
2
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
 
Sample Output
6
-1
 
 
 
//low方法应该是一点一点的找,但是,运用KMP算法会变牛逼。。(KMP竟然还可以用到数组中!!(⊙o⊙)
//这题很有趣,让我知道算法更重要的是思想,不要有局限自己思维
 
 
#include <iostream>
#include <cstdio> using namespace std;
int data1[],data2[];
int nex[],n,m; void getnext()
{
int i=,j=-;
nex[]=-;
while(i<m)
{
if(j==-||data2[i]==data2[j])
{
i++;j++;
if(data2[i]==data2[j])
nex[i]=nex[j];
else
nex[i]=j;
}
else
j=nex[j];
}
} int kmp()
{
int i=,j=;
getnext();
while(i<n)
{
if(j==-||data1[i]==data2[j])
{
i++;j++;
}
else
{
j=nex[j];// j=nex[i]这里又记错了。。。。
}
if(j==m)
return i-m+;
}
return -;
} int main()
{
int l;
cin>>l;
while(l--)
{
cin>>n>>m;
for(int i=;i<n;i++)
scanf("%d",&data1[i]);
for(int i=;i<m;i++)
scanf("%d",&data2[i]);
getnext();
int ans=kmp();
cout<<ans<<endl; }
return ;
}
 

HDU1711:Number Sequence的更多相关文章

  1. 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 ...

  2. ACM1005:Number Sequence

    Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  3. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

  4. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  6. HDU 1711:Number Sequence(KMP模板,求位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu5014:number sequence对称思想

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.... ...

  8. 【HDU1711】Number Sequence

    题面 大致题意: 给定两个数列A,B,长度分别为N和M 求出 满足 Ak=B1 ,Ak+1=B2......Ak+M-1=Bm 的最小k值 如果有多个k值输出最小的一个 题解 KMP裸题 直接计算B数 ...

  9. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

随机推荐

  1. Node.js入门 NPM

    参考一 Node入门  七天学会NodeJS  Node.js v4.2.4 手册 & 文档  Node.js 教程 node.js摸石头系列 从零开始学习node.js   What is ...

  2. 模仿快递路线图的html, css 样式

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. NetPayClient for PHP使用说明

    名 称 放置的路径 用 途 SecssUtil.class.php 根据项目工程的需要放置对应路径下 支持PHP5.4.8及以上版本 用于提供商户签名.验签.加密.解密.文件验签等方法调用 Mer.p ...

  4. 设计模式 -- 解释器模式(Interpreter Pattern)

    2015年12月15日00:19:02 今天只看了理论和demo,明天再写文章,跑步好累 2015年12月15日21:36:00 解释器模式用来解释预先定义的文法. <大话设计模式>里面这 ...

  5. Struts2第二天

    Struts2第二天 昨天: 1.Action的编写方式:实现Action接口.继承ActionSupport.自定义pojo作为action 2.action调用方法:默认的execute.meth ...

  6. 2016 Technology

    rem 62.5%在chrome谷歌和其他浏览器不一样 http://www.5imoban.net/jiaocheng/div+css/201512191529.html http://blog.c ...

  7. [jquery备忘]

    has :包含,找元素里面的子元素(单个) <div><span>123</span></div> $('div').has('span').css() ...

  8. UITableView简单使用

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不 ...

  9. 安卓.点击头像-->编辑个人姓名-->提交后.同时调用js关闭页面-->返回上一层

    $(document).ready(function() { $('#selfbtn').click(function(){ var u = navigator.userAgent; if (u.in ...

  10. gcc 编译

    ../gcc-5.2.0/configure --enable-threads=posix --disable-checking --disable-multilib --enable-languag ...