Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15017    Accepted Submission(s):
6585

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
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 10010
  4. int n,m;
  5. int b[MAX],a[MAX*100];
  6. int next[MAX];
  7. void getmap()
  8. {
  9. int i;
  10. for(i=0;i<n;i++)
  11. scanf("%d",&a[i]);
  12. for(i=0;i<m;i++)
  13. scanf("%d",&b[i]);
  14. }
  15. void getfail()
  16. {
  17. int i,j;
  18. next[0]=next[1]=0;
  19. for(i=1;i<m;i++)
  20. {
  21. j=next[i];
  22. while(j&&b[i]!=b[j])
  23. j=next[j];
  24. next[i+1]=b[i]==b[j]?j+1:0;
  25. }
  26. }
  27. void kmp()
  28. {
  29. int i,j=0;
  30. int ok=0;
  31. for(i=0;i<n;i++)
  32. {
  33. while(j&&a[i]!=b[j])
  34. j=next[j];
  35. if(a[i]==b[j])
  36. j++;
  37. if(j==m)
  38. {
  39. ok=1;
  40. printf("%d\n",i-m+2);//难点在这里
  41. return ;
  42. }
  43. }
  44. if(!ok)
  45. printf("-1\n");
  46. }
  47. int main()
  48. {
  49. int i,t;
  50. scanf("%d",&t);
  51. while(t--)
  52. {
  53. scanf("%d%d",&n,&m);
  54. getmap();
  55. getfail();
  56. kmp();
  57. }
  58. return 0;
  59. }

  

hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】的更多相关文章

  1. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  2. HDOJ 1711 Number Sequence

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

  3. HDU 1711 Number Sequence(KMP匹配数字串)

    这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...

  4. HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...

  5. HDU 1711 Number Sequence(数列)

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

  6. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  7. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  8. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  9. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

随机推荐

  1. 灵活运用绑定变量---declare匿名块使用绑定变量

    declare        type cur01 is ref cursor;     v_cur cur01;        v_match123 varchar2(2000);        v ...

  2. 40个DBA日常维护的SQL脚本--1113

    from itpub --1.查询碎片程度高的表--条件为什么block>100,因为一些很小的表,只有几行数据实际大小很小,但是block一次性分配就是5个(11g开始默认一次性分配1M的bl ...

  3. 第一次用Github desktop(mac)提交代码遇到的问题

    1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...

  4. 嵌入式css样式,写在当前的文件中

    现在有一任务,把下面的“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短词文字字号修改为18px. 如果用内联式css样式的方法进行设置将是一件很头疼的事情(为每一个<span>标签 ...

  5. 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理

    Web页面中经常碰到这类问题,就是客户端多次点击一个按钮或者链接,导致程序出现不可预知的麻烦. 客户就是上帝,他们也不是有意要给你的系统造成破坏,这么做的原因很大一部分是因为网络慢,点击一个操作之后, ...

  6. yii2源码学习笔记(六)

    Behvaior类,Behavior类是所有事件类的基类: 目录yii2\base\Behavior.php <?php /** * @link http://www.yiiframework. ...

  7. PHP 开启 ssh2

    首先,为PHP安装SSH2扩展需要两个软件包, libssh2和ssh2(php pecl拓展). 两者的最新版本分别为libssh2-1.5.0.tar.gz和ssh2-0.12.tgz,下载地址分 ...

  8. python自动开发之第十三天

    1.Paramiko模块下的demo.py程序     前面利用Python中的Paramiko模块可以进行SSH的连接,以及用来传送文件(SFTP),但是无论是哪一种方式,连接都是短暂的,并非是长连 ...

  9. 造成session丢失的原因和解决方法

    win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程,   造成保存在该进程中的session丢失. 因为Session,Appl ...

  10. 求助:对话框下OnInitDialog中使用SetTimer无效

    原文地址:http://www.w3c.com.cn/%E6%B1%82%E5%8A%A9%EF%BC%9A%E5%AF%B9%E8%AF%9D%E6%A1%86%E4%B8%8Boninitdial ...