poj 3461 - Oulipo 经典kmp算法问题
2017-08-13 19:31:47
writer:pprp
对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了)
所以这道题就作为一个模板,为大家使用吧。
题目大意:给你一个子串P和一个主串S,求在主串中有多少个子串?
代码如下:(需要注意的点我都标记好了,两个函数可以直接用)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> using namespace std;
int ans;
const int maxn = ;
int next[maxn];
char S[maxn],P[maxn]; //构造next数组
void get_next()
{
int i = ;
int j = -; int lenp = strlen(P); //要用额外变量,如果写在while循环中就会TLE next[] = -; while(i < lenp)
{
if(j == - || P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
} //开始模式匹配
void kmp()
{
int i = ;
int j = ; //要用额外变量,如果写在while循环中就会TLE
int lens = strlen(S);
int lenp = strlen(P); get_next(); //这个构造不能忘记写 while(i < lens && j < lenp)
{
if(j == - || P[j] == S[i])
{
i++;
j++;
}
else
j = next[j];
if(j == lenp)
{
ans++;
j = next[j];
}
}
}
int main()
{
int cas;
cin >> cas; while(cas--)
{
ans = ;
memset(next,,sizeof(next));
scanf("%s%s",P,S);
kmp();
cout << ans << endl;
}
return ;
}
poj 3461 - Oulipo 经典kmp算法问题的更多相关文章
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(——KMP算法)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- poj 3461 Oulipo,裸kmp
传送门 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32373 Accepted: 13093 Desc ...
- poj 3461 Oulipo(KMP模板题)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36903 Accepted: 14898 Descript ...
- POJ 3461 Oulipo(KMP裸题)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- poj 3461 Oulipo(kmp统计子串出现次数)
题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...
- poj 3461 Oulipo(KMP)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49378 Accepted: 19617 Descript ...
- 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 ...
随机推荐
- Zipline入门教程
Zipline Beginner Tutorial Basics 基础 Zipline is an open-source algorithmic trading simulator written ...
- 数据库 - 初识MySQL - 介绍/安装
一.介绍: mysql数据库管理软件: 套接字:服务端,客户端:客户端可访问服务端得数据 1.支持并发:操作得是共享得数据 2.处理锁,数据安全,性能 3.用别人得软件,得照着别人得规范,组织自己得语 ...
- Scala函数特性
通常情况下,函数的參数是传值參数:即參数的值在它被传递给函数之前被确定.可是,假设我们须要编写一个接收參数不希望立即计算.直到调用函数内的表达式才进行真正的计算的函数. 对于这样的情况.Scala提供 ...
- mmu介绍
arm exynos4412平台mmu配置以及的简单创建. 1.mmu的全称是Memory Management Unit(内存管理单元) 2.mmu所在位置.mmu再arm核心.他不是一个外设,他是 ...
- setup.py
from distutils.core import setup # 使用说明 # 执行以下命令 # python3 setup.py build # python3 setup.py sdist(这 ...
- kettle部分传输场景应用(每个作业都实验过啦)
不过都是全量的,没有增量的,增量的需要自行写脚本实现 1.mysql->mysql 2.ftp->mysql(整个文件夹下面读取) 3.hdfs->mysql 4.sftp-> ...
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- [golang note] 内建类型
基础类型 √ golang内建基础类型有布尔类型.整数类型.浮点类型.复数类型.字符串类型.字符类型和错误类型. 复合类型 √ golang支持的复合类型有指针.数组.数组切片.字典.通道.结构体和接 ...
- testng生成报告 testng-xslt 美化测试报告
testng生成报告 testng-xslt 美化测试报告 testng生成报告 testng-xslt 美化测试报告 用TestNG测试后,自动会生成html的测试报告.利用 testNG-xslt ...
- AlphaControls的使用方法
AlphaControls的使用方法 转载▼ 一.安装方法: 1.解压缩下载的文件,并把它放到你希望的位置,例如 D:\Coder\ 2.在Delphi的菜单:Tools->Envir ...