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 ...
随机推荐
- 行为链分析zipkin
行为链分析zipkin - 跑zipkin python例子 https://github.com/openzipkin/pyramid_zipkin-example # get the latest ...
- GCC:/usr/lib/libstdc++.so.6: version GLIBCXX_3.4.15 not found
源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found的问题.这 ...
- 【Java】设计模型-五种单例模型
一. 什么是单例模式 只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计. 单例模式的主要作用是保证在Java程序中,某个类只有一个实例存在. 单例模式有很多好处,它能够 ...
- BlackHat Arsenal USA 2018 ToolsWatch黑客工具库
原文链接:https://medium.com/hack-with-github/black-hat-arsenal-usa-2018-the-w0w-lineup-7de9b6d32796 Blac ...
- 快速入门 WePY 小程序【转】
一.WePY介绍 WePY 是 腾讯 参考了Vue 等框架对原生小程序进行再次封装的框架,更贴近于 MVVM 架构模式, 并支持ES6/7的一些新特性. 二.WePY 使用 1.WePY的安装或更新都 ...
- Java编程的逻辑 (91) - Lambda表达式
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- mybatis 映射器(mappers) 配置说明 加载映射文件方式
映射器(mappers) 既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这 ...
- Rk3288 双屏异显单触摸
系统版本:RK3288 android 5.1 设备同时有两个lcd,主屏是mipi接口,带有触摸屏,触摸屏是usb接口,副屏是hdmi接口,没有触摸屏,正常情况下,两个lcd显示相同内容,触摸屏一切 ...
- mac xmind 激活
下载地址 https://www.jb51.net/softjc/624167.html 打开压缩包中的[K].zip 按里面的READ ME!.rtf 文件来操作 嗯,就这样
- 从头配置,开发 cocos2dx 3.14.1
要使用cocos2dx写一个麻将测试工具. 配置流程如下: 1. 下载安装最新版本的VS(VS2017): https://www.visualstudio.com/zh-hans/downloads ...