A - Number Sequence
InputThe 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 a11, a22, ...... , aNN. The third line contains M integers which indicate b11, b22, ...... , bMM. All integers are in the range of −1000000,1000000−1000000,1000000.
OutputFor 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
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<vector>
- #include<string>
- #include<cstring>
- using namespace std;
- #define MAXN 1000001
- typedef long long LL;
- /*
- KMP 查找子串首次出现的位置
- */
- int s[MAXN],t[MAXN],Next[MAXN];
- void kmp_pre(int m)
- {
- int j,k;
- j=;k=-;Next[]=-;
- while(j<m)
- {
- if(k==-||t[j]==t[k])
- Next[++j] = ++k;
- else
- k = Next[k];
- }
- }
- int KMP(int n,int m)
- {
- int i,j,ans;
- i=j=ans=;
- kmp_pre(m);
- if(n==&&m==)
- return (s[]==t[])?:-;
- for(i=;i<n;i++)
- {
- while(j>&&s[i]!=t[j])
- j = Next[j];
- if(s[i]==t[j])
- j++;
- if(j>=m)
- {
- if(i-m+>)
- return i-m+;
- else
- return -;
- }
- }
- return -;
- }
- int main()
- {
- int T,n,m;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- for(int i=;i<n;i++)
- scanf("%d",&s[i]);
- for(int i=;i<m;i++)
- scanf("%d",&t[i]);
- printf("%d\n",KMP(n,m));
- }
- }
A - Number Sequence的更多相关文章
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence
Number Sequence A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...
- [AX]AX2012 Number sequence framework :(三)再谈Number sequence
AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
随机推荐
- jsp简单学习总结
以下均为jsp页面 1:<jsp:include page="index.jsp"/>相当于嵌入一个页面.还有一种是<frame src="main_l ...
- form表单点击后验证
function check(){ var customertype = document.getElementById("customertype"); //alert(cust ...
- 9.11NOIP模拟题
NOIP模拟赛 by thmyl 题目名称 superman market Lemon_Soda 可执行文件名 superman market Lemon_Soda 输入文件 superman.i ...
- 关于swoole 和golang 的压力测试结果
一.环境介绍 linux centos7 php7.1.18 go1.12.1 2核4G内存 二.代码 swoole代码 <?php $http = new swoole_http_server ...
- sql case when 用法
sql语言中有没有类似C语言中的switch case的语句?? 没有,用case when 来代替就行了. 例如,下面的语句显示中文年月 select ...
- ArcGIS Android工程迁移到其他电脑不能打开的问题
问题描述:当我把已经做好的ArcGIS Android工程想在其他电脑运行时,总是会提示报错.而报错的地方,正是出现在下面这条语句上. compile 'com.esri.arcgisruntime: ...
- C#入门经典 Chapter1&2
Chapter1 1.1 .Net Framework的内容 主要包含一个庞大的代码库,可以在客户端通过OOP来使用这些代码(OOP:Object Oriented Programming面对对象程序 ...
- SV creation order
SystemVerilog Instance Worlds When generating an UVM testbench and in particular the DUT - testbench ...
- CSS——padding
padding是盒子内容与边框的距离. padding:10px;/*上下左右都是10px*/ padding:10px 20px;/*上下是10px 左右是20px*/ padding:10px 2 ...
- [Windows Server 2008] 查看PHP详细错误信息
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:查看IIS ...