2016 年青岛网络赛---Family View(AC自动机)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5880
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."
The first line contains an integer n, represneting the size of the forbidden words list P. Each line of the next n lines contains a forbidden words Pi (1≤|Pi|≤1000000,∑|Pi|≤1000000) where Pi only contains lowercase letters.
The last line contains a string T (|T|≤1000000).
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.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define N 1000005
using namespace std;
char str[];
int v[];
int head,tail; struct node
{
node *fail;
node *next[];
int count;
node()
{
fail=NULL;
count=;
for(short i=;i<;i++)
next[i]=NULL;
}
}*q[N];
node *root;
void insert(char *str) ///建立Trie
{
int temp,len;
node *p=root;
len=strlen(str);
for(int i=;i<len;i++)
{
temp=str[i]-'a';
if(p->next[temp]==NULL)
p->next[temp]=new node();
p=p->next[temp];
}
p->count=len;
}
void setfail() ///初始化fail指针,BFS
{
q[tail++]=root;
while(head!=tail)
{
node *p=q[head++];
node *temp=NULL;
for(short i=;i<;i++)
if(p->next[i]!=NULL)
{
if(p==root) ///首字母的fail必指向根
p->next[i]->fail=root;
else
{
temp=p->fail; ///失败指针
while(temp!=NULL) ///2种情况结束:匹配为空or找到匹配
{
if(temp->next[i]!=NULL) ///找到匹配
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) ///为空则从头匹配
p->next[i]->fail=root;
}
q[tail++]=p->next[i]; ///入队;
}
}
} void query()
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int index;
if(str[i]>='A'&&str[i]<='Z') index=str[i]-'A';
else if(str[i]>='a'&&str[i]<='z') index=str[i]-'a';
else { p=root; continue; }
while(p->next[index]==NULL&&p!=root) ///跳转失败指针
p=p->fail;
p=p->next[index];
if(p==NULL)
p=root;
node *temp=p; ///p不动,temp计算后缀串
while(temp!=root)
{
if(temp->count>)
{
v[i-temp->count+]++;
v[i+]--;
break;
}
temp=temp->fail;
}
}
return ;
} int main()
{
int T, num;
scanf("%d",&T);
while(T--)
{
for(int i=;i<tail;i++)
free(q[i]);
memset(v,,sizeof(v));
head=tail=;
root = new node();
scanf("%d", &num);
getchar();
for(int i=;i<num;i++)
{
gets(str);
insert(str);
}
setfail();
gets(str);
int len=strlen(str),sum=;
query();
for(int i=;i<len;i++)
{
sum+=v[i];
if(sum<=) printf("%c",str[i]);
else printf("*");
}
puts("");
}
return ;
}
2016 年青岛网络赛---Family View(AC自动机)的更多相关文章
- 2016 年青岛网络赛---Tea
		
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5881 Problem Description Tea is good. Tea is life. Te ...
 - 2016 年青岛网络赛---Sort(k叉哈夫曼)
		
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...
 - hdu 5881 Tea (2016 acm 青岛网络赛)
		
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others) Me ...
 - HDU 5884 Sort(2016年青岛网络赛 G 二分+贪心+小优化)
		
好题 题意:给你n<=100000个数,每个数范围[0,1000],然后给你一个最大的代价T,每次最多合并k个数成为一个数,代价为k个数的总和.问最后合成1个数的总代价不大于T的最小k 题解:我 ...
 - HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)
		
题目链接 2016 青岛网络赛 Problem C 题意 给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...
 - 2016ACM-ICPC Qingdao Online青岛网络赛题解
		
TonyFang+Sps+我=5/12 滚了个大粗 2016年9月21日16:42:36 10题完工辣 01 题意:求形同的数中大于n的最小值 题解:预处理所有的(5194个),在这里面二分 #inc ...
 - HDU5880 Family View ac自动机第二题
		
Steam is a digital distribution platform developed by Valve Corporation offering digital rights mana ...
 - 6.29 省选模拟赛 坏题 AC自动机 dp 图论
		
考场上随手构造了一组数据把自己卡掉了 然后一直都是掉线状态了. 最后发现这个东西不是subtask -1的情况不多 所以就没管无解直接莽 写题有点晚 故没调出来.. 考虑怎么做 容易想到建立AC自动机 ...
 - HDU5880 Family View(2016青岛网络赛 AC自动机)
		
题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...
 
随机推荐
- PHP面向对象05_接口与多态
			
抽象类与接口 2014-9-2 9:14:32 摘要: 1.抽象类(abstract) 2.接口(interface) 3.多态的应用 1.抽象类(abstract) 特点: 1)抽象类使用abstr ...
 - Android开发学习之路-自定义ListView(继承BaseAdapter)
			
大三学生一个,喜欢编程,喜欢谷歌,喜欢Android,所以选择的方向自然是Android应用开发,开博第一篇,希望以后会有更多的进步. 最近在做一个记账App的时候,需要一个Activity来显示每个 ...
 - 基于 flow.ci 实现 PHP 项目自动化持续集成
			
高效程序员的习惯之一--让开发流程自动化.Automating shapes smarter future. 这是一个关于如何快速实现 PHP 项目自动化持续集成的快速指导.无论你是否使用过持续集成, ...
 - Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
			
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
 - jQuery_01之选择器
			
1.jQuery对象获取:①先获得DOM对象,再用$函数封装到jQuery对象:var $jQuery=$(DOM对象):②直接使用$函数查找到的DOM对象,被自动封装到jQuery对象中:var $ ...
 - echart饼状图使用,打发时间。
			
新公司,刚来几天,闲着没事,领导让我做些无关痛痒的活,优化报表统计!!!之前是用flash做的,现在要改成echart实现.好吧,之前没用过,抱着学习态度,研究了下.写点东西打发下时间,能帮到需要帮助 ...
 - 自己动手写中文分词解析器完整教程,并对出现的问题进行探讨和解决(附完整c#代码和相关dll文件、txt文件下载)
			
中文分词插件很多,当然都有各自的优缺点,近日刚接触自然语言处理这方面的,初步体验中文分词. 首先感谢harry.guo楼主提供的学习资源,博文链接http://www.cnblogs.com/harr ...
 - JavaScript禁用页面刷新
			
JavaScript禁用页面刷新代码如下: //禁用F5刷新 document.onkeydown = function () { if (event.keyCode == 116) { event. ...
 - C#对称加密(AES加密)每次生成的密文结果不同思路代码分享
			
思路:使用随机向量,把随机向量放入密文中,每次解密时从密文中截取前16位,其实就是我们之前加密的随机向量. 代码 public static string Encrypt(string plainTe ...
 - Net任意String格式转换为DateTime类型
			
方式一:Convert.ToDateTime(string) Convert.ToDateTime(string) 注意:string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方式二: ...