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的更多相关文章

  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. 搭建本地wordpress

    1.首先,下载xampp,安装按默认勾选即可. 2.安装完成后,启动Apache和MySQL这两个服务. 启动后变成绿色,表示启动成功. 3.点击MySQL项的Admin进入数据库后台. 4.点击用户 ...

  2. vue杂记

    VUE杂记 声明式渲染 <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app' ...

  3. Android中Application类总结

    本文出处: 炎之铠csdn博客:http://blog.csdn.net/totond 炎之铠邮箱:yanzhikai_yjk@qq.com 本文原创,转载请注明本出处! 前言 最近的开发中经常使用到 ...

  4. python socket 接口

    一.简介 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求.socket起 ...

  5. BZOJ 1941: [Sdoi2010]Hide and Seek KDtree + 估价函数

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...

  6. Echarts特效散点图全解

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  7. 解决Webpack中提示syntax 'classProperties' isn't currently enabled的错误

    当我们使用了一些JavaScript的一些新特性的时候,但是有没有在webpack.config.js里面或者是.babelrc文件中配置相关插件,就可以解决了. error:Support for ...

  8. C++项目作业 学生管理系统

    /*Student.h*/#pragma once #include<string.h> using namespace std; #include<string> class ...

  9. The Forth Week

    1.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. cp -r /etc/skell /home/tuser1 ; ...

  10. 小白两篇博客熟练操作MySQL 之 第一篇

    小白两篇博客熟悉操作MySQL  之   第一篇 一.概述 1. 什么是数据库? 答: 储存数据的仓库, 如: 在ATM的事例中创建的一个db 目录, 称为数据库 2. 什么是Mysql, Oracl ...