POJ3461 Oulipo 字符串
正解:kmp/哈希
解题报告:
这题其实就kmp板子,,,用来复习下kmp的太久没打了QAQ
所以kmp做法就不港了放个代码就是了QAQ
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
#define il inline
#define gc getchar()
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i) const int N=+;
int nxt[N];
char w[N],t[N]; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il void getnxt()
{
ri lth=strlen(w+);
rp(i,,lth)
{
ri nw=nxt[i-];
while(nw && w[nw+]!=w[i])nw=nxt[nw];
if(w[nw+]==w[i])++nw;nxt[i]=nw;
}
}
il int kmp()
{
ri lthw=strlen(w+),ltht=strlen(t+),i=,j=,as=;
while(j<=ltht)
{
if(w[i]==t[j]){++i;++j;if(i==lthw+)++as,i=nxt[i-]+;}
else if(i==)++j;else i=nxt[i-]+;
}
return as;
} int main()
{
// freopen("3461.in","r",stdin);freopen("3461.out","w",stdout);
ri T=read();
while(T--)
{
memset(nxt,,sizeof(nxt));
scanf("%s",w+);scanf("%s",t+);
getnxt();printf("%d\n",kmp());
}
return ;
}
然后大概港下哈希滴做法趴
就直接读入,然后对他们分别做哈希,判断一下就好
啊说得有点苍白,,,放下代码趴QAQ
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
#define il inline
#define gc getchar()
#define ri register int
#define rb register bool
#define rc register char
#define ll unsigned long long
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i) const int N=+,bas=;
int lth_w,lth_t;
ll w_hsh,t_hsh[N],poww;
char w[N],t[N]; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il ll power(ri gd,ri gs){ll ret=;while(gs){if(gs&)ret=1ll*ret*gd;gd=1ll*gd*gd;gs>>=;}return ret;} int main()
{
// freopen("3461.in","r",stdin);freopen("3461.out","w",stdout);
ri T=read();
while(T--)
{
ri as=;w_hsh=;poww=;
scanf("%s",w+);scanf("%s",t+);lth_w=strlen(w+);lth_t=strlen(t+);
rp(i,,lth_w)w_hsh=1ll*w_hsh*bas+w[i];
rp(i,,lth_t)t_hsh[i]=1ll*t_hsh[i-]*bas+t[i];
//poww=power(bas,lth_w);
rp(i,,lth_w)poww=poww*bas;
rp(i,lth_w,lth_t)if(w_hsh==t_hsh[i]-t_hsh[i-lth_w]*poww)++as;
printf("%d\n",as);
}
return ;
}
有个小细节要注意,写在下面了QwQ
注意一下的是,那个poww不能快速幂,,,会玄学出错,,,就快速幂乘出来的结果都和直接乘不一样,,,我也布吉岛为什么但反正注意一下就是了QAQ
POJ3461 Oulipo 字符串的更多相关文章
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- POJ-3461 Oulipo(KMP,模式串在主串中出现次数)
题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...
- POJ3461 Oulipo KMP算法
这个算法去年的这个时候就已经听过了,看毛片算法哈哈..不过理解它确实花了我很久的时间..以致于我一直很排斥字符串的学习,因为总觉得太难了,但是有些硬骨头还是要啃的,这个寒假就啃啃字符串还有一些别的东西 ...
- poj3461 Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- poj3461 Oulipo —— KMP
题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...
- POJ3461——Oulipo
1.题目大意:单字符串匹配问题 2.分析:经典KMP问题 存个模板QAQ #include <cstdio> #include <cstdlib> #include <c ...
- 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 ...
随机推荐
- [CI]jenkins安装&插件管理&java-helloworld之旅
持续集成概述 没有持续集成时的情况 持续集成最佳实战 维护一个单一的代码库 使构建自动化 执行测试是构建的一部分 集成日志及历史记录 使用统一的依赖包管理库 每天至少集成一次 jenkins实现持续集 ...
- 基于Kafka的生产者消费者消息处理本地调试
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/68174111冷血之心的博客) Kafka下载地址:http://d ...
- IOS-企业开发人员账号&邓白氏码申请记录
Apple开发人员账号分三种,个人.公司,还有企业.个人和公司都称为标准账号. 另一种是教育机构的账号. 账号介绍 个人和公司的就不说了.如今仅仅说企业账号 首先是申请企业账号的地址: https:/ ...
- 【转载】多模式串匹配之AC自动机
原文地址:https://www.cnblogs.com/codeape/p/3845375.html 目录 [隐藏] 一.概述 二.AC算法思想 三.字典树tire的构造 四.搜索路径的确定 附录: ...
- 如何在云端部署SAP HANA实战, Azure 上的 SAP HANA(大型实例)概述和体系结构
什么是 Azure 上的 SAP HANA(大型实例)? Azure 上的 SAP HANA(大型实例)是一种针对 Azure 的独特解决方案. 除了提供 Azure 虚拟机以用于部署和运行 SAP ...
- Python代码转换为exe可执行程序详解
1:安装pyinstaller pip install pyinstaller 2,制作exe 1,先写一个hello.py print('hello world!') input() 2.执行(在s ...
- 【GIS】GeoHash
- ansible运维工具(一)
运维工具介绍 OS Provisioning: PXE, Cobbler(repository, distritution,profile) PXE: dhcp, tftp, (http, ftp) ...
- linux signal
1) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联. 登录Linux时,系统会分配给 ...
- [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...