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 ...
随机推荐
- spring data jpa 遇到的问题
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.O ...
- 【opencv安裝】opencv2和opencv3共存——安装opencv2和opencv3到指定目录
安装 opencv2和opencv3共存会导致运行时问题,须分开 下载源码 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/hom ...
- servlet实现多文件打包下载
当用户一次下载多个文件时.普通情况是,每下载一个文件,均要弹出一个下载的对话框.这给用户造成了非常大不便. 比較理想的情况是,用户选择多个文件后.server后端直接将多个文件打包为zip.以下贴出实 ...
- 015-HQL中级5-hive创建索引
索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...
- 记CM+kerberos环境停电后无法启动报错An error: (java.security.PrivilegedActionException: javax.security.sasl.SaslException: GSS initiate failed [Caused by GSSException: No valid credentials provided (Mechanism leve
公司突然停电,然后cm环境无法重启,报错 An error: (java.security.PrivilegedActionException: javax.security.sasl.SaslExc ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- sublime2常用设置
设置文本字体格式 • Preferences -> Setting-User • 加入设置:"font_face" : "courier new", &q ...
- (转)C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
程序在内存有五个存在区域: A:动态区域中的栈区 B:动态区域中的栈区 C:静态区域中:全局变量 和静态变量 (这个区域又可以进一步细分为:初始化的全局变量和静态变量 以及 未初始 ...
- 过滤adb logcat 日志
原文地址http://blog.csdn.net/listening_music/article/details/7518990 另外比较好的文章http://blog.csdn.net/liao27 ...
- 基于java的网络爬虫框架(实现京东数据的爬取,并将插入数据库)
原文地址http://blog.csdn.net/qy20115549/article/details/52203722 本文为原创博客,仅供技术学习使用.未经允许,禁止将其复制下来上传到百度文库等平 ...