HDU5880 Family View
Posted ---学习ing---
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5880 Family View相关的知识,希望对你有一定的参考价值。
Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and social networking services. A family view can help you to prevent your children access to some content which are not suitable for them.
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."
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=1000010;
int Next[maxn][26],End[maxn],fail[maxn],h[maxn];
int cnt,root=1,ans;
int q[10000000],tail,head;
int a[maxn],sum[maxn];
char c[maxn] ;
void _init()
{
//不要memset只要200ms
memset(a,0,sizeof(a));
ans=0;cnt=1;fail[root]=-1;//root的fail不能等于本身,不然不能重新开始。
head=tail=0;End[root]=0;
for(int i=0;i<26;i++) Next[root][i]=0;
}
void _insert(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=0;i<L;i++){
if(!Next[Now][s[i]-‘a‘]) {
Next[Now][s[i]-‘a‘]=++cnt;
fail[cnt]=0;
End[cnt]=0;
for(int i=0;i<26;i++) Next[cnt][i]=0;
h[cnt]=h[Now]+1;
a[cnt]=0;
}
Now=Next[Now][s[i]-‘a‘];
}
End[Now]++;
}
void _build()//bfs
{
q[++head]=root;
while(tail<head){
int Now=q[++tail];
for(int i=0;i<26;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=0;i<L;i++){
int x=-1;
if(c[i]>=‘A‘&&c[i]<=‘Z‘) x=c[i]-‘A‘;
if(c[i]>=‘a‘&&c[i]<=‘z‘) x=c[i]-‘a‘;
if(x==-1) {
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]>0){
a[i-h[tmp]+1]-=1;
a[i+1]+=1;
break;
}
tmp=fail[tmp];
}
}
sum[0]=a[0];
for(int i=1;i<L;i++){
sum[i]=sum[i-1]+a[i];
}
for(int i=0;i<L;i++) {
if(sum[i]<0) 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=1;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
s[0]=getchar();
gets(c);
_build();
_query();//文章
}
return 0;
}
以上是关于HDU5880 Family View的主要内容,如果未能解决你的问题,请参考以下文章