poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461
先来一发KMP算法:
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <string>
- #include <iostream>
- #include <cmath>
- #include <map>
- #include <queue>
- using namespace std;
- #define ls(rt) rt*2
- #define rs(rt) rt*2+1
- #define ll long long
- #define ull unsigned long long
- #define rep(i,s,e) for(int i=s;i<e;i++)
- #define repe(i,s,e) for(int i=s;i<=e;i++)
- #define CL(a,b) memset(a,b,sizeof(a))
- #define IN(s) freopen(s,"r",stdin)
- #define OUT(s) freopen(s,"w",stdin)
- const int MAXN = 1000000+100;
- char T[MAXN],P[MAXN],tmp[MAXN];//T--文本,P--模板串
- int fail[MAXN];
- void getfail()
- {
- int m=strlen(P);
- fail[0]=fail[1]=0;
- for(int i=1;i<m;i++)
- {
- int j=fail[i];
- while(j && P[i]!=P[j])j=fail[j];
- fail[i+1]=P[i]==P[j]?j+1:0;
- }
- }
- int Find()
- {
- int ans=0;
- int n=strlen(T),m=strlen(P);
- if(n<m)
- {
- strcpy(tmp,T);
- strcpy(T,P);
- strcpy(P,tmp);
- }
- getfail();
- int j=0;
- for(int i=0;i<n;i++)
- {
- while(j && P[j]!=T[i])j=fail[j];
- if(P[j] == T[i])j++;
- if(j==m)//printf("%d\n",i-m+1);//find it
- ans++;
- }
- return ans;
- }
- int main()
- {
- //IN("poj3461.txt");
- int n;
- while(~scanf("%d",&n))
- {
- while(n--)
- {
- scanf("%s%s",P,T);
- printf("%d\n",Find());
- }
- }
- return 0;
- }
再来一发字符串HASH
我的字符串HASH模板在http://blog.csdn.net/u011026968/article/details/38460357
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <string>
- #include <iostream>
- #include <cmath>
- #include <map>
- #include <queue>
- using namespace std;
- #define ls(rt) rt*2
- #define rs(rt) rt*2+1
- #define ll long long
- #define rep(i,s,e) for(int i=s;i<e;i++)
- #define repe(i,s,e) for(int i=s;i<=e;i++)
- #define CL(a,b) memset(a,b,sizeof(a))
- #define IN(s) freopen(s,"r",stdin)
- #define OUT(s) freopen(s,"w",stdin)
- #define ull unsigned long long
- const ull B = 1e8+7; /*according to the book*/
- const int MAXN = 1000000+100;
- char a[MAXN],b[MAXN],tmp[MAXN];
- int hashfind()
- {
- int ans=0;
- int al=strlen(a),bl=strlen(b);
- if(al>bl)
- {
- strcpy(tmp,a);
- strcpy(a,b);
- strcpy(b,tmp);
- }
- ull t=1,ah=0,bh=0;
- for(int i=0;i<al;i++)
- {
- t*=B;
- ah=ah*B+a[i];
- bh=bh*B+b[i];
- }
- for(int i=0;i+al<=bl;i++)
- {
- if(ah==bh)ans++;
- if(i+al<bl)bh=bh*B+b[i+al]-b[i]*t;
- }
- return ans;
- }
- int main()
- {
- //IN("poj3461.txt");
- int n;
- while(~scanf("%d",&n))
- {
- while(n--)
- {
- scanf("%s%s",a,b);
- printf("%d\n",hashfind());
- }
- }
- return 0;
- }
poj 3461 字符串单串匹配--KMP或者字符串HASH的更多相关文章
- POJ 3461 Oulipo(字符串匹配,KMP算法)
题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...
- 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 ...
- 【kmp】 字符串最大周期
大侠住店 TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 116 Accepted: 64 Description 有一天晚上,一位 ...
- POJ - 3461 (kmp)
题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- 字符串匹配算法之 kmp算法 (python版)
字符串匹配算法之 kmp算法 (python版) 1.什么是KMP算法 KMP是三位大牛:D.E.Knuth.J.H.MorriT和V.R.Pratt同时发现的.其中第一位就是<计算机程序设计艺 ...
- Python 细聊从暴力(BF)字符串匹配算法到 KMP 算法之间的精妙变化
1. 字符串匹配算法 所谓字符串匹配算法,简单地说就是在一个目标字符串中查找是否存在另一个模式字符串.如在字符串 "ABCDEFG" 中查找是否存在 "EF" ...
- KMP求字符串最小循环节
证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即 i ...
- 统一修改表单参数(表单提交的空字符串统一转null)
统一修改表单参数(表单提交的空字符串统一转null) 1.介绍: 我们业务中有时会遇到提交的表单中某个参数为空字符串,导致后台接受的为空字符串("")而不是我们理想中的null,会 ...
- 数据结构学习之字符串匹配算法(BF||KMP)
数据结构学习之字符串匹配算法(BF||KMP) 0x1 实验目的 通过实验深入了解字符串常用的匹配算法(BF暴力匹配.KMP.优化KMP算法)思想. 0x2 实验要求 编写出BF暴力匹配.KM ...
随机推荐
- ndk书写位置的问题
defaultConfig { applicationId "com.chenql.helloandroidjni" minSdkVersion 22 targetSdkVersi ...
- Ubuntu14.4安装mysql
一.安装 apt-get install mysql-server mysql-client 设置用户名和密码 二.检查 sudo service mysql restart 三.支持 1.apach ...
- 排序算法JavaScript版
冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (va ...
- unable to get system library for the project
当向eclipse导入项目实例后,项目上出现红叉的错误提示,在项目属性里的Java Build Path里发现了错误提示复选选项: unable to get system library for t ...
- SpringMVC与MyBatis整合方法
一.springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在s ...
- [转载]java中Date,SimpleDateFormat
一.Java中的日期概述: 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在Java ...
- 栈和队列问题:设计一个有 getMin 功能的栈
[知识点] 栈是一个先进后出(FILO-First In Last Out)的数据结构,队列是一种先进先出(FIFO-First In First Out)的数据结构. [题目] 实现一个特殊的栈,在 ...
- Java 初学者
在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...
- Flask - 路由系统
目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...
- poj 2031
#include<stdio.h> #include<math.h> #include<stdlib.h> #define N 200 double co(doub ...