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 ...
随机推荐
- Istio 1.1部署实践
前提条件 正确安装配置Kubernetes集群 CentOS Linux release 7.5.1804 安装 下载istio 1.1版本 [root@vm157 ~]# wget https:// ...
- E20170626-hm
authenticate vt. 认证,证明是真实的.可靠的或有效的; 鉴定,使生效; author n. 作者; 著作家; 创造者; 发起人;
- bzoj4563: [Haoi2016]放棋子(错排+高精)
4563: [Haoi2016]放棋子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 387 Solved: 247[Submit][Status] ...
- 微信小程序压缩图片并上传到服务器(拿去即用)
这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...
- ACM_汉诺塔问题(递推dp)
Problem Description: 最近小G迷上了汉诺塔,他发现n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了 ...
- CSS发布时间
* 1996年W3C正式推出了CSS1.* 1998年W3C正式推出了CSS2.* CSS2.1是W3C现在正在推荐使用的.* CSS3现在还处于开发中.
- Parameter index out of range (1 > number of parameters, which is 0).
数据库错误:Parameter index out of range (1 > number of parameters, which is 0) ...
- cmd bat 相对命令
"%~dp0",在BAT中,是不是“相对路径”的意思 (2013-08-21 12:19:32) 转载▼ 标签: 杂谈 分类: C# 0念 零 ,代表你的批处理本身. d p是FO ...
- 最新省市区划分码code
爬取国家统计局省市区code 提供php爬取脚本以及json和sql https://github.com/zzDylan/area-code 觉得好用给个star,3q
- 1B课程笔记分享_StudyJams_2017
课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...