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. scala优点以及eclipse上安装scala插件

    可拓展 (面向对象,函数式编程) 静态类型化 (可检验,安全重构) 兼容JAVA (类库调用,互操作) 支持并发控制 (强计算能力,自定义其他控制结构) 语法简洁 (代码行短,类型推断,抽象控制) 插 ...

  2. sublime修改代码字体颜色

    1.首先知道你采用的是什么颜色主题在Preferences => color scheme查看,假设是Monokai2.查找该主题的脚本文件Preferences => Browse Pa ...

  3. CodeForces 699A Launch of Collider

    枚举相邻两个$a[i]$与$a[i+1]$,如果$s[i]=R$并且$s[i+1]=L$,那么$i$和$i+1$会碰撞,更新答案. #pragma comment(linker, "/STA ...

  4. HTML通过事件传递参数到js一

    原文链接:http://bbs.51cto.com/thread-1098421-1-1.html 目标处理函数为selectAttr(test) 1.直接传递给定参数如: onclick=" ...

  5. 3、Hibernate三态间的转换

    学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别 ...

  6. 7、Spring+Struts2+MyBaits(无映射接口实现类)

    1.创建userinfo.sql数据库脚本 create table userinfo (id ), name ), password ), telephone ), isadmin )); --4. ...

  7. OpenGL多视口

    #include <gl/glew.h> #include <gl/freeglut.h> #include <iostream> ; ; float rtri = ...

  8. POJ 2083 Fractal 分形

    去年校赛团队赛就有一道分形让所有大一新生欲生欲死…… 当时就想学了 结果一直拖到…… 今天上午…… 马上要省选了 才会一点基础分形…… 还是自己不够努力啊…… 分形主要是要找到递归点…… 还有深度…… ...

  9. Chapter 15_2 编写模块的基本方法

    在Lua中创建一个模块最简单的方法是:创建一个table. 并将所有需要导出的函数放入其中,最后返回这个table. 下例中的inv声明为程序块的局部变量,就是将其定义成一个私有的名称: local ...

  10. Unlocker(强力删除文件工具) 1.9.2 汉化绿色版

    软件名称: Unlocker(强力删除文件工具) 1.9.2 汉化绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 5 ...