HDU5880 Family View ac自动机第二题
Take an MMORPG game as an example, given a sentence T, and a list of forbidden words {P}, your job is to use '*' to subsititute all the characters, which is a part of the substring matched with at least one forbidden word in the list (case-insensitive).
For example, T is: "I love Beijing's Tiananmen, the sun rises over Tiananmen. Our great leader Chairman Mao, he leades us marching on."
And {P} is: {"tiananmen", "eat"}
The result should be: "I love Beijing's *********, the sun rises over *********. Our gr*** leader Chairman Mao, he leades us marching on."
InputThe first line contains the number of test cases. For each test case:
The first line contains an integer nn, represneting the size of the forbidden words list PP. Each line of the next nn lines contains a forbidden words Pi (1≤|Pi|≤1000000,∑|Pi|≤1000000)Pi (1≤|Pi|≤1000000,∑|Pi|≤1000000) where PiPi only contains lowercase letters.
The last line contains a string T (|T|≤1000000)T (|T|≤1000000).OutputFor each case output the sentence in a line.Sample Input
1
3
trump
ri
o
Donald John Trump (born June 14, 1946) is an American businessman, television personality, author, politician, and the Republican Party nominee for President of the United States in the 2016 election. He is chairman of The Trump Organization, which is the principal holding company for his real estate ventures and other business interests.
Sample Output
D*nald J*hn ***** (b*rn June 14, 1946) is an Ame**can businessman, televisi*n pers*nality, auth*r, p*litician, and the Republican Party n*minee f*r President *f the United States in the 2016 electi*n. He is chairman *f The ***** *rganizati*n, which is the p**ncipal h*lding c*mpany f*r his real estate ventures and *ther business interests.
AC自动机+sum数组扫描记录“*段”
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
int Next[maxn][],End[maxn],fail[maxn],h[maxn];
int cnt,root=,ans;
int q[],tail,head;
int a[maxn],sum[maxn];
char c[maxn] ;
void _init()
{
//不要memset只要200ms
// memset(a,0,sizeof(a));
ans=;cnt=;fail[root]=-;//root的fail不能等于本身,不然不能重新开始。
head=tail=;End[root]=;
for(int i=;i<;i++) Next[root][i]=;
}
void _insert(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=;i<L;i++){
if(!Next[Now][s[i]-'a']) {
Next[Now][s[i]-'a']=++cnt;
fail[cnt]=;
End[cnt]=;
for(int i=;i<;i++) Next[cnt][i]=;
h[cnt]=h[Now]+;
a[cnt]=;
}
Now=Next[Now][s[i]-'a'];
}
End[Now]++;
}
void _build()//bfs
{
q[++head]=root;
while(tail<head){
int Now=q[++tail];
for(int i=;i<;i++){
if(Next[Now][i]){
if(Now==root) fail[Next[Now][i]]=root;
else{
int p=fail[Now];
while(p){//给儿子们找对象
if(Next[p][i]){
fail[Next[Now][i]]=Next[p][i];
break;//找到了就停止
}
p=fail[p];//配对
}
if(!p) {
fail[Next[Now][i]]=root;//重新开始
}
}
q[++head]=Next[Now][i];
}
}
}
}
void _query()
{
int L=strlen(c);
int Now=root;
for(int i=;i<L;i++){
int x=-;
if(c[i]>='A'&&c[i]<='Z') x=c[i]-'A';
if(c[i]>='a'&&c[i]<='z') x=c[i]-'a';
if(x==-) {
Now=root;
continue;
}
while(Now!=root&&!Next[Now][x]) Now=fail[Now];
Now=Next[Now][x];
if(!Now) Now=root;//即使失败,也不气馁,一切从零开始
int tmp=Now;
while(tmp!=root){
if(End[tmp]>){
a[i-h[tmp]+]-=;
a[i+]+=;
break;
}
tmp=fail[tmp];
}
}
sum[]=a[];a[]=;
for(int i=;i<L;i++){
sum[i]=sum[i-]+a[i];
a[i]=;
}
for(int i=;i<L;i++) {
if(sum[i]<) putchar('*');
else putchar(c[i]);
}
putchar('\n');
}
int main()
{
char s[maxn];
int n,j,i,T;
scanf("%d",&T);
while(T--){ _init();
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
s[]=getchar();
gets(c); _build();
_query();//文章
}
return ;
}
HDU5880 Family View ac自动机第二题的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- springMVC注解的入门案例
1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...
- python x[:] x[::]用法总结
python x[:] x[::]用法总结 X[:,0] # 二维数组取第1维所有数据 X[:,1] # 第2列 X[0,:] # 第1行 X[3,:] # 第三行 X[1:4,:] # 第一二三行 ...
- LA 3295 数三角形
https://vjudge.net/problem/UVALive-3295 题意: 数出n行m列的网格顶点能组成多少个三角形. 思路: 直接去数的话比较麻烦,这道题目是可以重复的,只要位置不同就可 ...
- 24,25-request对象
var http = require('http'); var server = http.createServer(); server.listen() console.log(server.add ...
- UART速度的问题
1 原来UART实验的,速度被设置成9600,因为UART,在计算速度的时候带的是96002 后来一次学习的时候,ADC用到串口打数据,那么他的串口速度任然是9600,原来用UBOOT中的速 度则是1 ...
- python3安装tensorflow遇到的问题
1. 使用命令:sudo pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow ...
- Codeforces Round #400
最近好像总是有点不想打,专题也刷不动,还是坚持这做了一场,虽然打到一半就没打了...(反正通常都只能做出两题) 感觉自己切水题越来越熟练了,然而难题还是不会做.. A题,水,用vector存下来就行了 ...
- Git 常用命令和 Git Flow 梳理
git 用 git 有一段时间了,之前没有详细地了解 git flow,导致协作过程中或多或少出现了一些头疼问题.最近静下心来理了下 git flow 的整个流程,再回头看开朗了不少,总结到这里.介绍 ...
- js判断回车,判断焦点控件
document.onkeydown=function(event){ e = event ? event :(window.event ? window.event : null); ...
- (1) iOS开发之UI处理-预览篇
不管是做iOS还是Android的开发,我想UI这块都是个大麻烦,任何客户端编程都是如此,我们要做的就是尽量减少我们工作的复杂度,这样才能更轻松的工作. 在iOS开发中Xcode虽然自带了强大的IB( ...