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. 配置 Apache 的虚拟主机

    1.在host配置比如: 找到记事本以管理员的身份打开,然后文件->打开  C:\Windows\System32\drivers\etc    下面的hosts文件 127.0.0.1 www ...

  2. log4j配置示例

    在配置文件中按包名或类名来定义Logger 在程序中按类名取Logger 定义: log4j.rootLogger=debug,stdout log4j.logger.com.mypkg=debug, ...

  3. oracle 行专列

    首先,做准备工作. 建表 -- Create table create table DEMO ( n_iden NUMBER, c_order_code NVARCHAR2(), c_order_na ...

  4. oracle导入dmp数据库文件

    要用sys账户登录数据库,创建和dmp文件一样的表空间名称 1. 创建表空间 例如: create tablespace test(表空间名称) datafile 'F:\oracle\oradata ...

  5. Chapter 2 Open Book——25

    "My name is Edward Cullen," he continued. "I didn't have a chance to introduce myself ...

  6. [妙味Ajax]第二课:实例:留言板、瀑布流

    知识点总结 瀑布流原理(固定布局) 总宽度大小固定 每列宽度固定,比如LI,高度自动计算,每列使用float:left来布局 计算最短的一列,将Div插到最短的一列处(li里面包div)(getSho ...

  7. css布局理解

    1.盒模型(页面的所有元素都看成一个类似于礼品盒的盒子:包括块级元素和行内元素(也叫内联元素)):http://www.cnblogs.com/cchyao/archive/2010/07/12/17 ...

  8. 一个ubuntu命令

      curl 获取web curl www.baidu.com

  9. js函数的一些技巧

    1 函数的引用可以直接通过函数名来调用 bind: function (fn, scope) { return function () { return fn.apply(scope, argumen ...

  10. 《高性能Javascript》读书笔记-3

    第三章 DOM编程 把dom和js 各自想象为一个岛,他们之间用收费的桥梁链接,每次访问dom都必须途径这座桥收取过路费,访问次数多费用就高了.所有必须减少来往次数. innerHtml 与dom比较 ...