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 ...
随机推荐
- vs2013cs页面的代码太长,除了方法,没有折叠,如何处理
VS再带一款插件,工具->扩展和更新,然后选择"联机",在搜索框搜索C# outline 2013,然后安装重启vs即可
- mybatis-plus忽略映射字段
mybatis-plus使用对象属性进行SQL操作,经常会出现对象属性非表字段的情况,忽略映射字段使用以下注解: @TableField(exist = false):表示该属性不为数据库表字段,但又 ...
- GRE封装解封装过程
GRE(Generic Routing Encapsulation,通用路由封装)协议是对某些网络层协议(IPX, AppleTalk, IP,etc.)的数据报文进行封装,使这些被封装的数据报文能够 ...
- Wireshark安装使用及报文分析(图文详解)
Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与很多其他网络工具一样,Wireshark也使用pcapnetwork l ...
- 关于v$librarycache的几个字段含义
对v$librarycache中的get,pin和reload的含义: Gets: (Parse) The number of lookups for objects of the namespace ...
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- a排兵布阵
来源hdu1166 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵 ...
- E - Closest Common Ancestors
Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u, ...
- ubuntu的apt-get install的默认安装路径(转)
一.apt-get 安装 deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb或者直接双击此文件 dpkg 是Debian ...
- centos7邮件服务器SSL配置
在上篇文章centos7搭建postfix邮件服务器的搭建中我们没有配置SSL,接下来我们在这篇文章中讲讲centos7邮件服务器SSL配置. 1. 创建SSL证书 [root@www ~]# cd ...