题意:
模拟敏感词和谐器,T 组数据,每组数据 N 个子串,一个母串,输出和谐的母串
思路:
母串带空格,用gets就行,用子串的长度做标记,匹配时若匹配到,和谐掉’标记数‘个数个字符就行。
代码:
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct Trie
{
int next[1000100][26],fail[1000100],end[1000100];
int root,L;
int newnode()
{
for(int i = 0; i < 26; i++)
next[L][i] = -1;
end[L++] = -1;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
if(next[now][buf[i]-'a'] == -1)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now] = len;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = 0; i < 26; i++)
if(next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while( !Q.empty() )
{
int now = Q.front();
Q.pop();
for(int i = 0; i < 26; i++)
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
void query(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
if(buf[i]>='A'&&buf[i]<='Z') now = next[now][buf[i]-'A'];
else if(buf[i]>='a'&&buf[i]<='z') now = next[now][buf[i]-'a'];
else now = root;
int temp = now;
while( temp != root )
{
if(end[temp] != -1){
for(int j = 1;j <= end[temp];j++)
buf[i-j+1] = '*';
}
temp = fail[temp];
}
}
puts(buf);
return ;
}
};
char buf[1000100];char str[1000100];
Trie ac;
int main()
{
ios::sync_with_stdio(false);
int n,T;
scanf("%d",&T);
while( T-- )
{
scanf("%d",&n);
ac.init();
gets(str);
for(int i = 0; i < n; i++)
{
gets(str);
ac.insert(str);
}
ac.build();
gets(buf);
ac.query(buf);
}
return 0;
}