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 ...
随机推荐
- meterpreter中使用mimikatz获取windows密码
进去meterpreter后getuid一下 这获得系统管理员权限 开始 加载mimikatz模块 load mimikatz 加载成功. 第一种方法: 获取登录密码的hash值 msv 上面已经是得 ...
- python溴事百科爬虫
import urllib.request import re # qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374896 def jokeCr ...
- xx局点FusionCloud6.3 type1 计算配额失败问题
现象: 排查过程: 1.登录云平台部署面,选择部署资源-服务器 2.找到MOC-ManageOne-Service01.MOC-ManageOne-Service02两台机器ip地址. 3.用ssh工 ...
- 台式机ATX电源:各接口定义、启动方法、电源特点
ATX,英文全称:Advanced Technology Extended,是一种由Intel公司在1995年公布的PC机主板结构规范. ATX电源作用是把交流220V的电源转换为计算机内部使用的直流 ...
- CF1553X Harbour.Space Scholarship Contest 2021-2022 (Div. 1 + Div. 2)
掉大分 E 对于一个序列,把它排回去的最小次数是 $\sum置换环大小-1=错位个数-置换环个数$ 注意到m小于等于n/3.那么最多修正2m个错位.正确位置的个数必须大于等于n/3才可能在m次内修正. ...
- OSPF的五种报文
OSPF的五种报文 Hello报文 DD(Database Description)数据库描述报文 LSR(LinkState Request)链路状态请求报文 LSU(LinkState Updat ...
- 【算法】两个list合并
转载博客地址 http://blog.sina.com.cn/s/blog_5da93c8f0101fdrp.html 有两个ArrayList,分别为list1和list2,分析这两个list后生成 ...
- 攻防世界supersqli
supersqli 补充知识点 rename 命令格式: rename table 原表名 to 新表名 例如,在表myclass名字更改为youclass: mysql>rename tabl ...
- 简单vue项目脚手架
简单vue项目脚手架 github地址 使用技术栈 webpack(^2.6.1) webpack-dev-server(^2.4.5) vue(^2.3.3) vuex(^2.3.1) vue-ro ...
- 大数据学习之路之ambari配置(三)
添加了虚拟机内存空间 重装ambari