题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1

 
分析:应该是最简单的模板题了吧.....
代码如下:
==============================================================================================
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. const int MAXM = 1e4+;
  5. const int MAXN = 1e6+;
  6.  
  7. int a[MAXN], b[MAXM], next_b[MAXM];
  8.  
  9. void GetNext(int b[], int next[], int M)
  10. {
  11. int i=, j=-;
  12. next[] = -;
  13.  
  14. while(i < M)
  15. {
  16. if(j==- || b[i]==b[j])
  17. next[++i] = ++j;
  18. else
  19. j = next[j];
  20. }
  21. }
  22. int KMP(int a[], int b[], int next[], int N, int M)
  23. {
  24. int i = , j = ;
  25.  
  26. while(i < N)
  27. {
  28. while(j==- || a[i] == b[j] && j<M)
  29. i++, j++;
  30.  
  31. if(j == M)return i-M+;
  32.  
  33. j = next[j];
  34. }
  35.  
  36. return -;
  37. }
  38.  
  39. int main()
  40. {
  41. int T;
  42.  
  43. scanf("%d", &T);
  44.  
  45. while(T--)
  46. {
  47. int i, N, M;
  48.  
  49. scanf("%d%d", &N, &M);
  50.  
  51. for(i=; i<N; i++)
  52. scanf("%d", &a[i]);
  53. for(i=; i<M; i++)
  54. scanf("%d", &b[i]);
  55.  
  56. GetNext(b, next_b, M);
  57.  
  58. printf("%d\n", KMP(a, b, next_b, N, M));
  59. }
  60.  
  61. return ;
  62. }

Number Sequence - HDU 1711(KMP模板题)的更多相关文章

  1. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  2. (KMP 模板)Number Sequence -- Hdu -- 1711

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

  3. AC日记——Number Sequence hdu 1711

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

  4. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  5. HDU 2087 kmp模板题

    s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> ...

  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. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  8. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  9. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

随机推荐

  1. Centos ssh 登陆乱码解决办法

    1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030"LANGUAGE="zh_CN.GB18030:zh_CN.GB23 ...

  2. C#—委托分析

    1.简单委托示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; names ...

  3. (转)PHP正则表达式的快速学习方法

    1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...

  4. cxf WebService整理 (基于注解)

    http://blog.csdn.net/zjw10wei321/article/details/39889823

  5. 如果使用的是orm,是否还需要关系索引

    简而言之:是的,仍然需要理解索引,即使是使用对象关系映射(ORM)工具. ORM工具能够产生符合逻辑的,合法的查询(多数的时候),除非只是生成非常基本的查询(例如仅是根据主键查询的),否则它很难生成适 ...

  6. 文件上传-html

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>文件 ...

  7. angularjs学习笔记三——directive

    AngularJS 通过被称为 指令 的新属性来扩展 HTML. 正如你所看到的,AngularJS 指令是以 ng 作为前缀的 HTML 属性. HTML5 允许扩展的(自制的)属性,以 data- ...

  8. 转:Remote debugging with Visual Studio 2010

    Original URL http://www.codeproject.com/Articles/146838/Remote-debugging-with-Visual-Studio-2010 you ...

  9. Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b

    本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...

  10. WordPress网站更换老鹰主机详细操作

    眼看着之前买的虚拟主机就要到期了,本着节约至上的美德,就和同事一起买了老鹰主机.因为第一次网站的配置是一个朋友帮忙的,所以现在想完全自己动手操作,毕竟之后的博客维护还是得靠自己.下面就来和我一起学习怎 ...