Given two sequences of numbers : a11, a22, ...... , aNN, and b11, b22, ...... , bMM(1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11, aK+1K+1 = b22, ...... , aK+M−1K+M−1 = bMM. If there are more than one K exist, output the smallest one. 

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

  1. 2
  2. 13 5
  3. 1 2 1 2 3 1 2 3 1 3 2 1 2
  4. 1 2 3 1 3
  5. 13 5
  6. 1 2 1 2 3 1 2 3 1 3 2 1 2
  7. 1 2 3 2 1

Sample Output

  1. 6
  2. -1
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<vector>
  5. #include<string>
  6. #include<cstring>
  7. using namespace std;
  8. #define MAXN 1000001
  9. typedef long long LL;
  10. /*
  11. KMP 查找子串首次出现的位置
  12. */
  13. int s[MAXN],t[MAXN],Next[MAXN];
  14. void kmp_pre(int m)
  15. {
  16. int j,k;
  17. j=;k=-;Next[]=-;
  18. while(j<m)
  19. {
  20. if(k==-||t[j]==t[k])
  21. Next[++j] = ++k;
  22. else
  23. k = Next[k];
  24. }
  25. }
  26. int KMP(int n,int m)
  27. {
  28. int i,j,ans;
  29. i=j=ans=;
  30. kmp_pre(m);
  31. if(n==&&m==)
  32. return (s[]==t[])?:-;
  33. for(i=;i<n;i++)
  34. {
  35. while(j>&&s[i]!=t[j])
  36. j = Next[j];
  37. if(s[i]==t[j])
  38. j++;
  39. if(j>=m)
  40. {
  41. if(i-m+>)
  42. return i-m+;
  43. else
  44. return -;
  45. }
  46. }
  47. return -;
  48. }
  49. int main()
  50. {
  51. int T,n,m;
  52. scanf("%d",&T);
  53. while(T--)
  54. {
  55. scanf("%d%d",&n,&m);
  56. for(int i=;i<n;i++)
  57. scanf("%d",&s[i]);
  58. for(int i=;i<m;i++)
  59. scanf("%d",&t[i]);
  60. printf("%d\n",KMP(n,m));
  61. }
  62. }
 

A - Number Sequence的更多相关文章

  1. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...

  3. HDOJ 1711 Number Sequence

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

  4. Number Sequence

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

  5. [AX]AX2012 Number sequence framework :(三)再谈Number sequence

    AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...

  6. KMP - HDU 1711 Number Sequence

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

  7. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

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

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  10. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. Istio 1.1部署实践

    前提条件 正确安装配置Kubernetes集群 CentOS Linux release 7.5.1804 安装 下载istio 1.1版本 [root@vm157 ~]# wget https:// ...

  2. E20170626-hm

    authenticate   vt. 认证,证明是真实的.可靠的或有效的; 鉴定,使生效; author   n. 作者; 著作家; 创造者; 发起人;

  3. bzoj4563: [Haoi2016]放棋子(错排+高精)

    4563: [Haoi2016]放棋子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 387  Solved: 247[Submit][Status] ...

  4. 微信小程序压缩图片并上传到服务器(拿去即用)

    这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...

  5. ACM_汉诺塔问题(递推dp)

    Problem Description: 最近小G迷上了汉诺塔,他发现n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了 ...

  6. CSS发布时间

    * 1996年W3C正式推出了CSS1.* 1998年W3C正式推出了CSS2.* CSS2.1是W3C现在正在推荐使用的.* CSS3现在还处于开发中.

  7. Parameter index out of range (1 > number of parameters, which is 0).

    数据库错误:Parameter   index   out   of   range   (1   >   number   of   parameters,   which   is   0) ...

  8. cmd bat 相对命令

    "%~dp0",在BAT中,是不是“相对路径”的意思 (2013-08-21 12:19:32) 转载▼ 标签: 杂谈 分类: C# 0念 零 ,代表你的批处理本身. d p是FO ...

  9. 最新省市区划分码code

    爬取国家统计局省市区code 提供php爬取脚本以及json和sql https://github.com/zzDylan/area-code 觉得好用给个star,3q

  10. 1B课程笔记分享_StudyJams_2017

    课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...