2021.11.09 P4824 [USACO15FEB]Censoring S与P3121 [USACO15FEB]Censoring G(KMP&&AC自动机)
2021.11.09 P4824 [USACO15FEB]Censoring S与P3121 [USACO15FEB]Censoring G(KMP&&AC自动机)
https://www.luogu.com.cn/problem/P4824
题意:
给定字符串S和T,删除S中的T,形成新串,继续删除新串中的T,直至完全删除。
分析:
KMP求的是当前S的第i位能匹配到T的第j位,如果j==strlen(T)
删除stacki
中i-strlen(T)+1~i
,继续从S的i+1、k=f[stacki[top]]
与T开始继续匹配。
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int nexti[N],f[N],stacki[N],top;
char s[N],t[N];
inline void getnext(char *s){
int n=strlen(s);
int k=-1;nexti[0]=-1;
for(int i=0;i<n;){
while(s[i]!=s[k]&&k!=-1)k=nexti[k];
if(s[i]==s[k]||k==-1)nexti[++i]=++k;
}
}
int main(){
//freopen("P4824.out","w",stdout);
scanf("%s",s);
scanf("%s",t);
getnext(t);
//for(int i=1;i<=strlen(t);i++)cout<<nexti[i]<<" ";cout<<endl<<endl;
int k=0,n=strlen(s),m=strlen(t);
for(int i=0;i<=n;i++){
while(s[i]!=t[k]&&k!=-1)k=nexti[k];
if(s[i]==t[k]||k==-1)f[i]=++k;
stacki[++top]=i;
//cout<<top<<endl;
//for(int j=1;j<=top;j++)cout<<s[stacki[j]]<<" ";cout<<endl;
if(k==m)top-=m,k=f[stacki[top]];
//cout<<top<<endl;
//for(int j=1;j<=top;j++)cout<<s[stacki[j]];cout<<endl;// <<endl;
}
//for(int i=1;i<=strlen(s);i++)cout<<f[i]<<" ";cout<<endl;
for(int i=1;i<=top;i++)cout<<s[stacki[i]];
return 0;
}
https://www.luogu.com.cn/problem/P3121
题意:
给定字符串S和若干个T,删除S中的存在的T,形成新串,继续删除新串中存在的T,直至完全删除。
分析:
如上,只不过一对多需要用AC自动机,继续使用手工栈辅助。
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int m,top,stacki[N],f[N];
char s[N],t[N];
namespace trie{
int t[N][30],tot,val[N],fail[N];
queue<int>q;
void insert(char *s){
int len=strlen(s),u=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!t[u][v])t[u][v]=++tot;
u=t[u][v];
}
val[u]=len;
}
void build(){
for(int i=0;i<26;i++)if(t[0][i])q.push(t[0][i]);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<26;i++){
if(t[u][i])fail[t[u][i]]=t[fail[u]][i],q.push(t[u][i]);
else t[u][i]=t[fail[u]][i];
}
}
}
};
int main(){
scanf("%s",s);
cin>>m;
for(int i=1;i<=m;i++)scanf("%s",t),trie::insert(t);
trie::build();
int len=strlen(s),u=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
f[i]=u=trie::t[u][v];
stacki[++top]=i;
if(trie::val[u]){
top-=trie::val[u];
u=f[stacki[top]];
}
}
for(int i=1;i<=top;i++)cout<<s[stacki[i]];
return 0;
}
2021.11.09 P4824 [USACO15FEB]Censoring S与P3121 [USACO15FEB]Censoring G(KMP&&AC自动机)的更多相关文章
- 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)
2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...
- 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)
2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...
- 2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP)
2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP) https://www.luogu.com.cn/problem/P3435 题意: 对于一个仅 ...
- P4824 [USACO15FEB]Censoring (Silver) 审查(银)&&P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
P3121 [USACO15FEB]审查(黄金)Censoring (Gold) (银的正解是KMP) AC自动机+栈 多字符串匹配--->AC自动机 删除单词的特性--->栈 所以我们先 ...
- 2021.08.09 P7238 迷失森林(树的直径)
2021.08.09 P7238 迷失森林(树的直径) P7238 「DCOI」迷失森林 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.树的直径两种求法:两次dfs.树 ...
- ACM阶段总结(2016.10.07-2016.11.09)
来这里也有一段时间了…… 总感觉自己练得不是很有效. 最近的一些行动就是不断做比赛,然后不停地补,但是感觉这样像只无头苍蝇,没有效果,学不到什么真正的东西. 最近开始打算补专题,做做codeforce ...
- 「USACO15FEB」「LuoguP3121」审查(黄金)Censoring (Gold)(AC自动机
题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...
- 日常Javaweb 2021/11/19
Javaweb Dao层: //连接数据库,实现增查功能 package dao; import java.sql.Connection; import java.sql.DriverManager; ...
- 日常Java 2021/11/18
用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...
随机推荐
- Nginx--sub_filter模块
客户端浏览器访问替换html中的字符: location / { root /web/htdocs; random_index on; index index.html index.htm; subs ...
- mysql(mariadb)安装
mysql(mariadb)安装 前言 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将My ...
- 深度学习工具LabelXml安装教程
LabelXml安装教程 1,下载LabelXml工具 前往github上下载labelxml,官网地址如下:https://github.com/tzutalin/labelImg 下载打包源码为z ...
- Mysql学习笔记-InnoDB深度解析
前言 我们在上一篇博客聊了Mysql的整体架构分布,连接层.核心层.存储引擎层和文件系统层,其中存储引擎层作为Mysql Server中最重要的一部分,为我们sql交互提供了数据基础支持.存储引擎和文 ...
- 『忘了再学』Shell基础 — 3、echo命令的介绍与使用
目录 1.echo命令的作用 2.echo命令的基本用法 3.echo命令的-e选项用法 4.echo命令一些特殊用法 (1)输出字符带有字体颜色 (2)输出字符带有背景颜色 在讲Shell脚本之前, ...
- Hadoop-全分布式配置
目录 一.配置基础环境 1.配置网络信息 2.配置主机名 3.主机名与IP的映射关系 4.测试互通性 二.关闭防火墙和SELinux 1.关闭防火墙 2.关闭SELinux 三.安装 Hadoop 1 ...
- spring中bean的五种作用域?Spring中的bean是线程安全的吗?
spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...
- 解释基于 XML Schema 方式的切面实现?
在这种情况下,切面由常规类以及基于 XML 的配置实现.
- 什么是基于 Java 的 Spring 注解配置? 给一些注解的例子?
基于 Java 的配置,允许你在少量的 Java 注解的帮助下,进行你的大部分 Spring 配置而非通过 XML 文件. 以@Configuration 注解为例,它用来标记类可以当做一个 bean ...
- python-查找鞍点
[题目描述]对于给定5X5的整数矩阵,设计算法查找出所有的鞍点的信息(包括鞍点的值和行.列坐标,坐标从1开始). 提示:鞍点的特点:列上最小,行上最大. [练习要求]请给出源代码程序和运行测试结果 ...