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 ...
随机推荐
- ASP.NET 之 Chart Control for .Net Framework
ps:曾经靠着这张图做了很多的图形报表
- Spring Cloud (14) 服务网关-过滤器
Spring Cloud Zuul作为网关所具备的最基本的功能:路由,还具备另外一个核心的功能:过滤器. 过滤器 通过Spring Cloud Zuul实现的路由功能,我们的微服务可以通过统一的API ...
- Python随笔-快排
def swap(arr, i, j): temp = arr[i] arr[i] = arr[j] arr[j] = temp def part(arr, beg, end): : return b ...
- android黑科技系列——解析公众号文章消息和链接文章消息自动打开原理
一.辅助功能方案分析 关于WX的各种功能插件已经非常普遍了,而现在的插件都是依赖于Xposed框架进行的,所以个人觉得WX应该在这方便应对Xposed框架的使用防护,防止插件满天飞的现象,本文来介绍一 ...
- redis集群——RPLR简笔(Redis+PostgreSQL+Linux(centos7)+RabbitMQ)
使用的是centos7. 1.下载最新redis源码,解压(2016-05-12最新版本为3.2.0,3.0及以上才有官方集群) 2.进入源码根目录(此目录下的redis-stable目录),找到ut ...
- jQuery——属相操作
属性获取:attr(属性名), 属性设置:attr(属性名,具体值) 移除属性:removeAttr(属性名) 特殊情况:prop(属性名).prop(属性名,具体值):表单中状态属性checked. ...
- CSS——层叠性
层叠性:浏览器渲染是从上而下的,当多个样式作用于同一个(同一类)标签时,样式发生了冲突,总是执行后边的代码(后边代码层叠前边的代码).和标签调用选择器的顺序没有关系. <!DOCTYPE htm ...
- PHP 之base16加密解密封装
/** * @Description: base16加密 * @Author: Yang * @param $data * @return string */ function base16_enco ...
- Cesium学习笔记(五):3D 模型 (http://blog.csdn.net/umgsoil/article/details/74572877)
Cesium支持3D模型,包括关键帧动画,皮肤的改变还有单个节点的选择等,Cesium还提供了了一个基于网络的工具,将COLLADA模型转换为glTF,方便和优化模型添加 还记得我们在实体添加的时候添 ...
- 《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays
题目给定两个大小为 m 和 n 的有序数组nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...