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 ...
随机推荐
- Android Studio打包过程和应用安装过程
三个部分,检查项目和读取基本配置,Gradle Build,Apk Install和LaunchActivity. 应用安装到手机,会复制APK安装包到data/app目录下,解压并扫描安装包,把de ...
- “5W1H”带你来学习JavaScript
上次的设计模式讲课,从中学习到了非常多.不仅是技术上,更重要的是怎样来学习.我们学习的技术.科技的更新速度超过我们的想象,对于我们这个有生命年限的个体,怎样可以在有生之年可以让自己立足于科技的不败浪潮 ...
- [转]调试利器-SSH隧道
在开发微信公众号或小程序的时候,由于微信平台规则的限制,部分接口需要通过线上域名才能正常访问.但我们一般都会在本地开发,因为这能快速的看到源码修改后的运行结果.但当涉及到需要调用微信接口时,由于不和你 ...
- MySQL InnoDB 引擎的持久性与性能
MySQL 事务的 ACID 特性中,D 代表持久性(Durability):在使用 InnoDB 引擎时,当返回客户端一个成功完成事务的确认时, InnoDB 就会保证数据的一致性,即使该数据在此时 ...
- IntelliJ IDEA 主题、字体、编辑区主题、文件编码修改
主题修改 上图标注 1 所示为 IntelliJ IDEA 修改主题的地方,在 Windows 系统上 IntelliJ IDEA 默认提供的主题有四套:Darcula.IntelliJ.Window ...
- ORACLE拼日期
Oracle数据库拼字符串是用"||"连接的.在开发中,经常会用到时间范围的查询 例如 startTime >='2017-05-22 00:00:00' and endT ...
- Failed to execute 'write' on 'Document'动态载入的js不能执行write
统计代码一般都是直接一个标签,插入js,标签放在哪里,统计图表就放在哪里! 我现在是稍微改了一下,我自己加了一点js,在页面所有元素都加载完成之后我再动态的把统计js插入到我需要的地方. 统计代码的s ...
- 字节输入流:io包中的InputStream为所有字节输入流的父类。
字节输入流:io包中的InputStream为所有字节输入流的父类. Int read();读入一个字节(每次一个): 可先使用new byte[]=数组,调用read(byte[] b) read ...
- selenium+chromedriver刷点击量
#coding=utf-8 import re import time import json import requests from selenium import webdriver from ...
- ganglia问题汇总
1.有数据,不出图 排查方法: 1)确保 php-gd 插件已安装 2) 确保rrdtool 的命令路径是正确的 3)确保php.ini中passthru函数是否开启,参数safe_mode 是否为o ...