题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5880
Family View
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 862 Accepted Submission(s): 162
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
思路:其实差不多就是AC自动机的模板题。可是这个题目给的数据范围特别大,算一下空间复杂度,直接超内存了啊!就不敢写了,后来在网上看到别人开得那么大,也能够A掉。很是郁闷啊!于是,自己就去写了,写完之后,果断MLE啊!搞了好久没搞明白怎么回事!后来就去某群里问,问的时候,碰到了Q神。Q神说不要用memset一次性清空数组,超内存是我一次性memset数组导致的,将没有用的空间也用上了,于是试了下不一次性memset数组,果断不超了。呜呜,菜鸡太菜了!在此,感谢Q神的提示。需要注意的地方就是,每一次匹配成功,做出相应标记时,要标记整个匹配的串的范围,最后一遍扫过去即可。详见代码。
附上AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxnode = 1000005;
const int sigma_size = 26;
struct Aho_Corasick{
int child[maxnode][sigma_size], len[maxnode];
int f[maxnode], last[maxnode], cnt[maxnode];
bool value[maxnode];
int size;
void init(){
size = 1;
memset(child[0], 0, sizeof(child[0]));
memset(value, false, sizeof(value));
memset(cnt, 0, sizeof(cnt));
}
void insert(char * str){
int pos = 0;
for (int i=0; str[i]; ++i){
int id = str[i]-'a';
if (!child[pos][id]){
memset(child[size], 0, sizeof(child[size]));
child[pos][id] = size++;
}
pos = child[pos][id];
}
value[pos] = true;
len[pos] = strlen(str);
}
void get_fail(){
queue<int> q;
f[0] = 0;
for (int i=0; i<sigma_size; ++i){
int u = child[0][i];
if (u){
f[u] = 0;
q.push(u);
last[u] = 0;
}
}
while (!q.empty()){
int r = q.front();
q.pop();
for (int i=0; i<sigma_size; ++i){
int u = child[r][i];
if (!u){
child[r][i] = child[f[r]][i];
continue;
}
q.push(u);
int v = f[r];
while (v && !child[v][i])
v = f[v];
f[u] = child[v][i];
last[u] = value[f[u]] ? f[u] : last[f[u]];
}
}
}
void find(char * str){
int pos = 0;
for (int i=0; str[i]; ++i){
if (!isalpha(str[i]))
continue;
int id = tolower(str[i])-'a';
pos = child[pos][id];
if (value[pos])
count(pos, i);
else if (last[pos])
count(last[pos], i);
}
}
void count(int pos, int id){
if (pos){
++cnt[id+1];
--cnt[id-len[pos]+1];
count(last[pos], id);
}
}
} ac;
char str[maxnode];
int n;
int main(){
int T;
scanf("%d", &T);
while (T--){
scanf("%d", &n);
ac.init();
for (int i=0; i<n; ++i){
scanf("%s", str);
ac.insert(str);
}
ac.get_fail();
getchar();
fgets(str, maxnode, stdin);
ac.find(str);
int ans = 0;
for (int i=0; str[i]; ++i){
ans += ac.cnt[i];
if (ans < 0)
putchar('*');
else
putchar(str[i]);
}
}
return 0;
}