题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

思路:kmp模板,注意用scanf,不然超时。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn = ;
  6. int a[maxn],b[maxn],p[maxn],n,m;
  7. void pre()
  8. {
  9. int i,j;
  10. for(i=,j=;i<m;i++)
  11. {
  12. while(j>&&b[i+]!=b[j+]) j=p[j];
  13. if(b[i+]==b[j+]) j++;
  14. p[i+]=j;
  15. }
  16. }
  17. int kmp()
  18. {
  19. int i,j;
  20. for(i=,j=;i<n;i++)
  21. {
  22. while(j>&&a[i+]!=b[j+]) j=p[j];
  23. if(a[i+]==b[j+]) j++;
  24. if(j==m)
  25. {
  26. return i-j+;
  27. }
  28. }
  29. return -;
  30. }
  31. int main(void)
  32. {
  33. int t,i,j;
  34. scanf("%d",&t);
  35. while(t--)
  36. {
  37. scanf("%d%d",&n,&m);
  38. memset(p,,sizeof(p));
  39. for(i=;i<=n;i++) scanf("%d",&a[i]);
  40. for(i=;i<=m;i++) scanf("%d",&b[i]);
  41. pre();
  42. printf("%d\n",kmp());
  43. }
  44. return ;
  45. }

hdu-1711(kmp算法)的更多相关文章

  1. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  2. hdu 1686 KMP算法

    题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...

  3. hdu 4300 kmp算法扩展

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU 1711 kmp+离散化

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

  5. Number Sequence HDU 1711 KMP 模板

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

  6. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

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

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

  9. 字符串_KMP算法(求next[]模板 hdu 1711)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 问题描述:给两个序列a,b,长度分别为n,m(1<=n<=1000000,1< ...

  10. HDU 1711(KMP)字符串匹配

    链接  HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头   ...

随机推荐

  1. maven错误

    maven-enforcer-plugin (goal "enforce") is ignored by m2e. Plugin execution not covered by ...

  2. eclipse Jsp 自创建tags问题

    Can not find the tag directory "/WEB-INF/tags" 网上的说法有三种情况: 1. jsp2.0开始支持自定义tag,因此 web.xml文 ...

  3. pandas datafram重命名列名称

    方法一: 直接给column赋值 df.columns=["a", "b"], 所有的column全部重命名 example: import pandas as ...

  4. linux install redis-cli

    ubuntu: sudo apt-get install redis-cli centos: sudo -i yum list redis yum install redis>>> ...

  5. insert NULL into mysql

    https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are insertin ...

  6. How to Pronounce OF

    How to Pronounce OF Tagged With: OF Reduction Study the OF reduction.  There are many reductions in ...

  7. conductor 事件处理程序

    Introduction conductor中的事件提供工作流之间的松散耦合,并支持从外部系统生成和消耗事件. 包括: 1. 能够在外部系统像SQS或Conductor内部生成一个事件(消息). 2. ...

  8. ubuntu修改运行级别方法

    Ubuntu系统设置启动级别的问题,因自己以前遇到过,故做过笔记记录了下来:Ubuntu.Debian系列与RedHat.CentOS启动级别含义有所区别:Ubuntu系列运行级别定义如下:0 – H ...

  9. Mysql 内部默认排序

    mysql默认的排序: https://forums.mysql.com/read.php?21,239471,239688#msg-239688 Do not depend on order whe ...

  10. R画散点图、线型图、箱型图、直方图基本知识

    1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')