Family View
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1187 Accepted Submission(s): 234
Problem Description
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."
Input
The first line contains the number of test cases. For each test case:
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) .
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) .
Output
For 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.
Source
Recommend
题目大意:
给出模式串,给出一个句子。把句子中所有的模式串替换成 * 号,也就是关键字屏蔽。
思路:
在记录模式串终点的时候加上一个 当前串的长度。
在记录模式串终点的时候加上一个 当前串的长度。
用动态建树的方法要记得 delet 掉节点,然后就是程序时间比较慢,用 putchar 刚好,用 cout 会超时。论输入输出的重要性。
如果是 静态建树的方法,时间也差不多,都是 900+ 。
一开始是超内存,加了 Delet 开始超时,把 cout 改成putchar 开始 wa ,看了一个小时,发现数组没清空,mdzz。
Ac 代码:
<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define N 500010
char str[1000010], keyword[51];
int ans[1000010];
int head, tail;
struct node
{
node *fail;
node *next[26];
int cnt;
int ll;
node()
{
fail = NULL;
cnt = 0;
ll=0;
for(int i = 0; i < 26; ++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 = 0; i < len; ++i)
{
temp = str[i] - 'a';
if(p->next[temp] == NULL)
p->next[temp] = new node();
p = p->next[temp];
}
p->cnt=1;
p->ll=len;
}
void build_ac()
{
q[tail++] = root;
while(head != tail)
{
node *p = q[head++];
node *temp = NULL;
for(int i = 0; i < 26; ++i)
{
if(p->next[i] != NULL)
{
if(p == root)
p->next[i]->fail = root;
else
{
temp = p->fail;
while(temp != NULL)
{
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()
{
int index, len, result;
node *p = root;
result = 0;
len = strlen(str);
for(int i = 0; i < len; ++i)
{
if(str[i]>='A'&&str[i]<='Z')
index=str[i]-'A';
else if(str[i]>='a'&&str[i]<='z')
index = str[i] - 'a';
else
continue;
while(p->next[index] == NULL && p != root)
{
p = p->fail;
}
p = p->next[index];
if(p == NULL)
p = root;
node *temp = p;
while(temp != root)
{
if(temp->cnt)
{
ans[i+1]-=1;
ans[i-temp->ll+1]+=1;
break;
}
temp = temp->fail;
}
}
}
void Delete(node *rt)
{
if(rt == NULL) return;
for(int i = 0; i < 26; i++)
if(rt->next[i] != NULL) Delete(rt->next[i]);
delete(rt);
rt = NULL;
}
int main()
{
int ncase, num;
scanf("%d", &ncase);
while(ncase--)
{
head= tail = 0;
memset(ans,0,sizeof(ans));
root = new node();
scanf("%d", &num);
getchar();
for(int i = 0; i < num; ++i)
{
gets(keyword);
insert(keyword);
}
build_ac();
gets(str);
query();
int tt=0;
int tttt=strlen(str);
for(int i=0; i<tttt; i++)
{
tt+=ans[i];
if(tt<=0)
{
putchar(str[i]);
}
else
printf("*");
}
puts("");
Delete(root);
}
return 0;
}