http://poj.org/problem?id=3461

先来一发KMP算法:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <map>
  8. #include <queue>
  9. using namespace std;
  10.  
  11. #define ls(rt) rt*2
  12. #define rs(rt) rt*2+1
  13. #define ll long long
  14. #define ull unsigned long long
  15. #define rep(i,s,e) for(int i=s;i<e;i++)
  16. #define repe(i,s,e) for(int i=s;i<=e;i++)
  17. #define CL(a,b) memset(a,b,sizeof(a))
  18. #define IN(s) freopen(s,"r",stdin)
  19. #define OUT(s) freopen(s,"w",stdin)
  20.  
  21. const int MAXN = 1000000+100;
  22. char T[MAXN],P[MAXN],tmp[MAXN];//T--文本,P--模板串
  23. int fail[MAXN];
  24. void getfail()
  25. {
  26. int m=strlen(P);
  27. fail[0]=fail[1]=0;
  28. for(int i=1;i<m;i++)
  29. {
  30. int j=fail[i];
  31. while(j && P[i]!=P[j])j=fail[j];
  32. fail[i+1]=P[i]==P[j]?j+1:0;
  33. }
  34. }
  35.  
  36. int Find()
  37. {
  38. int ans=0;
  39. int n=strlen(T),m=strlen(P);
  40. if(n<m)
  41. {
  42. strcpy(tmp,T);
  43. strcpy(T,P);
  44. strcpy(P,tmp);
  45. }
  46. getfail();
  47. int j=0;
  48. for(int i=0;i<n;i++)
  49. {
  50. while(j && P[j]!=T[i])j=fail[j];
  51. if(P[j] == T[i])j++;
  52. if(j==m)//printf("%d\n",i-m+1);//find it
  53. ans++;
  54. }
  55. return ans;
  56. }
  57.  
  58. int main()
  59. {
  60. //IN("poj3461.txt");
  61. int n;
  62. while(~scanf("%d",&n))
  63. {
  64. while(n--)
  65. {
  66. scanf("%s%s",P,T);
  67. printf("%d\n",Find());
  68. }
  69.  
  70. }
  71. return 0;
  72. }

再来一发字符串HASH

我的字符串HASH模板在http://blog.csdn.net/u011026968/article/details/38460357

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <map>
  8. #include <queue>
  9. using namespace std;
  10.  
  11. #define ls(rt) rt*2
  12. #define rs(rt) rt*2+1
  13. #define ll long long
  14. #define rep(i,s,e) for(int i=s;i<e;i++)
  15. #define repe(i,s,e) for(int i=s;i<=e;i++)
  16. #define CL(a,b) memset(a,b,sizeof(a))
  17. #define IN(s) freopen(s,"r",stdin)
  18. #define OUT(s) freopen(s,"w",stdin)
  19. #define ull unsigned long long
  20. const ull B = 1e8+7; /*according to the book*/
  21. const int MAXN = 1000000+100;
  22. char a[MAXN],b[MAXN],tmp[MAXN];
  23.  
  24. int hashfind()
  25. {
  26. int ans=0;
  27. int al=strlen(a),bl=strlen(b);
  28. if(al>bl)
  29. {
  30. strcpy(tmp,a);
  31. strcpy(a,b);
  32. strcpy(b,tmp);
  33. }
  34. ull t=1,ah=0,bh=0;
  35. for(int i=0;i<al;i++)
  36. {
  37. t*=B;
  38. ah=ah*B+a[i];
  39. bh=bh*B+b[i];
  40. }
  41. for(int i=0;i+al<=bl;i++)
  42. {
  43. if(ah==bh)ans++;
  44. if(i+al<bl)bh=bh*B+b[i+al]-b[i]*t;
  45. }
  46. return ans;
  47. }
  48.  
  49. int main()
  50. {
  51. //IN("poj3461.txt");
  52. int n;
  53. while(~scanf("%d",&n))
  54. {
  55. while(n--)
  56. {
  57. scanf("%s%s",a,b);
  58. printf("%d\n",hashfind());
  59. }
  60.  
  61. }
  62. return 0;
  63. }

poj 3461 字符串单串匹配--KMP或者字符串HASH的更多相关文章

  1. POJ 3461 Oulipo(字符串匹配,KMP算法)

    题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...

  2. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  3. 【kmp】 字符串最大周期

    大侠住店 TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 116 Accepted: 64 Description 有一天晚上,一位 ...

  4. POJ - 3461 (kmp)

    题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  5. 字符串匹配算法之 kmp算法 (python版)

    字符串匹配算法之 kmp算法 (python版) 1.什么是KMP算法 KMP是三位大牛:D.E.Knuth.J.H.MorriT和V.R.Pratt同时发现的.其中第一位就是<计算机程序设计艺 ...

  6. Python 细聊从暴力(BF)字符串匹配算法到 KMP 算法之间的精妙变化

    1. 字符串匹配算法 所谓字符串匹配算法,简单地说就是在一个目标字符串中查找是否存在另一个模式字符串.如在字符串 "ABCDEFG" 中查找是否存在 "EF" ...

  7. KMP求字符串最小循环节

    证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即  i ...

  8. 统一修改表单参数(表单提交的空字符串统一转null)

    统一修改表单参数(表单提交的空字符串统一转null) 1.介绍: 我们业务中有时会遇到提交的表单中某个参数为空字符串,导致后台接受的为空字符串("")而不是我们理想中的null,会 ...

  9. 数据结构学习之字符串匹配算法(BF||KMP)

    数据结构学习之字符串匹配算法(BF||KMP) 0x1 实验目的 ​ 通过实验深入了解字符串常用的匹配算法(BF暴力匹配.KMP.优化KMP算法)思想. 0x2 实验要求 ​ 编写出BF暴力匹配.KM ...

随机推荐

  1. 微信接口本地调试(IIS服务器)

    1.下载ngrok,并注册获得token.官网下载地址:https://ngrok.com/ 如果你是在官网下载的,到后面映射域名的时候会要求购买他们的服务. 这里我们用一个国内免费的ngrok服务器 ...

  2. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...

  3. CSS3设计炫目字体

    阴影 .text-shadow{ text-shadow:#FF0000 0 0 10px; color:white; font-size:60px } 描边 <style> .text- ...

  4. 9-4前端vue面试的问题

    就没有什么顺序了,肖师傅的一些提问: 1- 配置文件中proxyTable的作用 2-@import '~styles/mixins.styl'  ~的作用 3-vue模拟的本地中访问地址的url带有 ...

  5. PHP 之websocket实现聊天室功能

    一.功能界面 具体的详细代码:https://github.com/yangsphp/websocket-master/tree/master 二.具体代码实现 1.前端代码如下 <!DOCTY ...

  6. codevs1961 躲避大龙

    1961 躲避大龙  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 你早上起来,慢悠悠地来到学校门口, ...

  7. exgcd扩展欧几里得求解的个数

    知识储备 扩展欧几里得定理 欧几里得定理 (未掌握的话请移步[扩展欧几里得]) 正题 设存在ax+by=gcd(a,b),求x,y.我们已经知道了用扩欧求解的方法是递归,终止条件是x==1,y==0: ...

  8. 网络:NAT使用场景

    NAT:Network Address Translation  网络地址转换 使用场景:家庭局域网,公司局域网的网络设备没有公网IP地址如何访问互联网? 简单图示: 理解一些原理: 1,互联网中网络 ...

  9. 44.bucket filter:统计各品牌最近一个月的平均价格

    课程大纲     GET /tvs/sales/_search { "size": 0, "query": { "term": { &quo ...

  10. 腾讯云,搭建LAMP服务

    lamp (Web应用软件) 编辑 Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被 ...