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. ndk书写位置的问题

    defaultConfig { applicationId "com.chenql.helloandroidjni" minSdkVersion 22 targetSdkVersi ...

  2. Ubuntu14.4安装mysql

    一.安装 apt-get install mysql-server mysql-client 设置用户名和密码 二.检查 sudo service mysql restart 三.支持 1.apach ...

  3. 排序算法JavaScript版

    冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (va ...

  4. unable to get system library for the project

    当向eclipse导入项目实例后,项目上出现红叉的错误提示,在项目属性里的Java Build Path里发现了错误提示复选选项: unable to get system library for t ...

  5. SpringMVC与MyBatis整合方法

    一.springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在s ...

  6. [转载]java中Date,SimpleDateFormat

    一.Java中的日期概述: 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在Java ...

  7. 栈和队列问题:设计一个有 getMin 功能的栈

    [知识点] 栈是一个先进后出(FILO-First In Last Out)的数据结构,队列是一种先进先出(FIFO-First In First Out)的数据结构. [题目] 实现一个特殊的栈,在 ...

  8. Java 初学者

    在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...

  9. Flask - 路由系统

    目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...

  10. poj 2031

    #include<stdio.h> #include<math.h> #include<stdlib.h> #define N 200 double co(doub ...