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)删除stackii-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自动机)的更多相关文章

  1. 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)

    2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...

  2. 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 题意: 你打算在纸上印一串字 ...

  3. 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 题意: 对于一个仅 ...

  4. P4824 [USACO15FEB]Censoring (Silver) 审查(银)&&P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

    P3121 [USACO15FEB]审查(黄金)Censoring (Gold) (银的正解是KMP) AC自动机+栈 多字符串匹配--->AC自动机 删除单词的特性--->栈 所以我们先 ...

  5. 2021.08.09 P7238 迷失森林(树的直径)

    2021.08.09 P7238 迷失森林(树的直径) P7238 「DCOI」迷失森林 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.树的直径两种求法:两次dfs.树 ...

  6. ACM阶段总结(2016.10.07-2016.11.09)

    来这里也有一段时间了…… 总感觉自己练得不是很有效. 最近的一些行动就是不断做比赛,然后不停地补,但是感觉这样像只无头苍蝇,没有效果,学不到什么真正的东西. 最近开始打算补专题,做做codeforce ...

  7. 「USACO15FEB」「LuoguP3121」审查(黄金)Censoring (Gold)(AC自动机

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

  8. 日常Javaweb 2021/11/19

    Javaweb Dao层: //连接数据库,实现增查功能 package dao; import java.sql.Connection; import java.sql.DriverManager; ...

  9. 日常Java 2021/11/18

    用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...

随机推荐

  1. Centos 7.5 通过yum安装GNOME Desktop时出现:file /boot/efi/EFI/centos from install of fwupdate-efi-12-5.el7.centos.x86_64 conflicts with file from package grub2-common-1:2.02-0.65.el7.centos.2.noarch

    系统版本为: [root@s10 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 由于管理kvm虚拟机的需求,需要安装 ...

  2. Java中获取applicationcontext(应用上下文)

    package com.wl.iwbservice.util; import org.springframework.beans.BeansException; import org.springfr ...

  3. 查找bug的一些经验总结

    项目开发中遇到的bug解决经验总结 今天在项目开发中遇到了两个很难解决的bug,我把我的思路记录下来,以供之后遇到bug时,提供一些思路: 编译通过,但总结"core dumped" ...

  4. github新手使用指南

    常用命令:   Git 速查表(摘自 AI有道) 一.常见命令 git init : 初始化 git 仓库,即将一个文件夹初始化为一个 git 仓库.具体的操作是创建一个 .git 隐藏文件夹 git ...

  5. 如何处理异形屏iphone X?

    safe area: 默认放置在安全区域以避免遮挡, 但会压缩 在meta中添加viewport-fit=cover: 告诉浏览器要讲整个页面渲染到浏览器中,不管设备是圆角与否,这个时候会造成页面的元 ...

  6. Listener是什么?有什么作用?

    Listener是指Servlet中的监听器. Listener可以对ServletContext对象.HttpSession对象.ServletRequest对象进行监听.

  7. 什么是 Ribbon负载均衡?

    (1)Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具. (2)Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等.简单的说,就是在配 ...

  8. Semaphore 有什么作用 ?

    Semaphore 就是一个信号量,它的作用是限制某段代码块的并发数.Semaphore 有一个构造函数,可以传入一个 int 型整数 n,表示某段代码最多只有 n 个线程可 以访问,如果超出了 n, ...

  9. MySQL_fetch_array 和 MySQL_fetch_object 的区别是什么?

    以下是 MySQL_fetch_array 和 MySQL_fetch_object 的区别: MySQL_fetch_array() – 将结果行作为关联数组或来自数据库的常规数组返回. MySQL ...

  10. java中的VO、PO、BO、DAO、POJO

     针对java工程里的各种带O的对象,进行分析,了解各自的作用. PO: persistent object,持久对象.与数据库里表字段一一对应.PO是一些属性,以及set和get方法组成.一般情况下 ...