POJ3461 Oulipo KMP算法
这个算法去年的这个时候就已经听过了,看毛片算法哈哈..不过理解它确实花了我很久的时间..以致于我一直很排斥字符串的学习,因为总觉得太难了,但是有些硬骨头还是要啃的,这个寒假就啃啃字符串还有一些别的东西吧,KMP的学习我看了好多好多博客才有那么些头绪,复杂度的分析更是无从谈起,不过线性匹配这样的算法实在太流弊了.~题目是水题,但也算是我的第一道KMP吧.~
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define mxt 1000005
#define mxp 10005
#define inf 0x3f3f3f3f int f[mxp+50];
char T[mxt+50];
char P[mxp+50]; void getFail(const char *P,int *f)
{
int m=strlen(P);
f[0]=f[1]=0;
for(int i=1;i<m;++i){
int j=f[i];
while(j&&P[i]!=P[j]) j=f[j];
f[i+1]= P[i]==P[j]? j+1:0;
}
} int KMP(const char *P,const char *T,int *f)
{
int cnt=0;int m=strlen(P),n=strlen(T);
getFail(P,f);int j=0;
for(int i=0;i<n;++i){
while(j&&P[j]!=T[i]) j=f[j];
if(P[j]==T[i]) ++j;
if(j==m) cnt++;
}
return cnt;
} int main()
{
int ca;cin>>ca;
while(ca--)
{
scanf("%s%s",P,T);
printf("%d\n",KMP(P,T,f));
}
return 0;
}
POJ3461 Oulipo KMP算法的更多相关文章
- [POJ] 3461 Oulipo [KMP算法]
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23667 Accepted: 9492 Descripti ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
- POJ 3461 Oulipo KMP算法(模板)
题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...
- POJ3461–Oulipo(KMP)
题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...
- poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- poj3461 Oulipo —— KMP
题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...
- KMP算法 hdu4686 Oulipo
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
随机推荐
- IOS做一个简单计算器
//声明非原创 步骤: 1.打开Xcode,单机Creat a new Xcode project 2.左边选择ios下Application,右边选择single view Application ...
- 水仙花数 java 实现
题目描述: 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求 ...
- Android模拟器中安装APK文件(转)
1.平台环境:Win7系统, 安装Eclipse,android4.0(sdk) 2.随便创建个工程(HelloWorld),结果如下: 3.运行(Run HelloWorld),启动模拟器,如下所示 ...
- JQuery在iframe中实现 点击后选中当前栏目的样式
二级或者三级折叠菜单参考http://www.cnblogs.com/qigege/p/5178947.html <script type="text/javascript" ...
- ASP.NET MVC 2 验证
来源:http://www.cnblogs.com/jhxk/articles/2612885.html 只为把自己觉的好的存起来 对用户输入的验证以及强制业务规则/逻辑是大多数web应用的核心需求 ...
- Asp.net MVC知识积累
一.知识积累 http://yuangang.cnblogs.com/ 跟蓝狐学mvc教程专题目录:http://www.lanhusoft.com/Article/169.html 依赖注入:htt ...
- ASP.NET Core 行军记 -----拔营启程
ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html ASP.NET Core :https://docs.asp.net/en/la ...
- Python开发【第一篇】Python基础之反射
反射 反射的作用:反射得作用是提高代码可读行. __import__导入模块和import导入模块的区别: __import__导入模块是通过字符串进行导入. import是常用得导入模块方法. 扩展 ...
- JNI 学习笔记
JNI是Java Native Interface的缩写,JNI是一种机制,有了它就可以在java程序中调用其他native代码,或者使native代码调用java层的代码.也 就是说,有了JNI我们 ...
- FastLoad错误 — RDBMS error 2634
我们来看一下下面这条语句: BEGIN LOADING stu_flERRORFILES error_1, error_2; 如果此时已经存在error_1或error_2表,那么将会报错,信息如 ...