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. jsp简单学习总结

    以下均为jsp页面 1:<jsp:include page="index.jsp"/>相当于嵌入一个页面.还有一种是<frame src="main_l ...

  2. form表单点击后验证

    function check(){ var customertype = document.getElementById("customertype"); //alert(cust ...

  3. 9.11NOIP模拟题

    NOIP模拟赛 by thmyl   题目名称 superman market Lemon_Soda 可执行文件名 superman market Lemon_Soda 输入文件 superman.i ...

  4. 关于swoole 和golang 的压力测试结果

    一.环境介绍 linux centos7 php7.1.18 go1.12.1 2核4G内存 二.代码 swoole代码 <?php $http = new swoole_http_server ...

  5. sql case when 用法

    sql语言中有没有类似C语言中的switch case的语句?? 没有,用case   when   来代替就行了.            例如,下面的语句显示中文年月         select  ...

  6. ArcGIS Android工程迁移到其他电脑不能打开的问题

    问题描述:当我把已经做好的ArcGIS Android工程想在其他电脑运行时,总是会提示报错.而报错的地方,正是出现在下面这条语句上. compile 'com.esri.arcgisruntime: ...

  7. C#入门经典 Chapter1&2

    Chapter1 1.1 .Net Framework的内容 主要包含一个庞大的代码库,可以在客户端通过OOP来使用这些代码(OOP:Object Oriented Programming面对对象程序 ...

  8. SV creation order

    SystemVerilog Instance Worlds When generating an UVM testbench and in particular the DUT - testbench ...

  9. CSS——padding

    padding是盒子内容与边框的距离. padding:10px;/*上下左右都是10px*/ padding:10px 20px;/*上下是10px 左右是20px*/ padding:10px 2 ...

  10. [Windows Server 2008] 查看PHP详细错误信息

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:查看IIS ...