- 題意:給定 P 個字串和一個文本 S,當文本 S 出現 P 個字串任何一個單字,就要進行加密。
- 題解:先對 P 個字串建立出 AC 自動機,接著在匹配 S 時,每當匹配到字串,就在該字串開始位置紀錄 hideEnd[x],為從 S[x] 開始,要加密多少字元數,因為有可能同時出現
ab
abc
都要加密的例子(一個字串是另一字串的前綴),所以 hineEnd 要取 max。算好 hideEnd 後,遍歷字串,用一個變數 tmp 維護目前需要加密多少字元數,當 tmp>0 時須加密。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int INF = 1e9;
const int MXN = 3e5 + 5;
const int MXS = 1e6 + 5;
const int MXW = 26;
const LL MOD = 10009;
const LL seed = 31;
cin.tie(NULL); \
cout.tie(NULL); \
ios_base::sync_with_stdio(false);
struct Node
{
int nodei;
char ch[MXS];
int sz[MXS];
int trie[MXS][MXW];
int fail[MXS];
void init() { nodei = -1; }
int newNode()
{
++nodei;
fail[nodei] = -1;
sz[nodei] = 0;
memset(trie[nodei], -1, sizeof(trie[nodei]));
return nodei;
}
// Node() { init(); }
} node;
void insert(int root, char *s, int id)
{
int sz = strlen(s);
FOR(i, 0, sz)
{
int v = s[i] - 'a';
if (node.trie[root][v] == -1)
{
node.trie[root][v] = node.newNode();
}
root = node.trie[root][v];
node.ch[root] = s[i];
}
node.sz[root] = strlen(s);
}
queue<int> q;
void bulidAC(int root)
{
int k, tmp;
FOR(i, 0, MXW)
{
if (node.trie[root][i] != -1)
{
node.fail[node.trie[root][i]] = root;
q.push(node.trie[root][i]);
}
}
while (!q.empty())
{
k = q.front();
q.pop();
node.sz[k] = max(node.sz[k], node.sz[node.fail[k]]);
FOR(i, 0, MXW) if (node.trie[k][i] != -1)
{
tmp = node.fail[k];
while (tmp != -1)
{
if (node.trie[tmp][i] != -1)
{
node.fail[node.trie[k][i]] = node.trie[tmp][i];
break;
}
tmp = node.fail[tmp];
}
if (tmp == -1)
{
node.fail[node.trie[k][i]] = root;
}
q.push(node.trie[k][i]);
}
}
}
int len[MXS], hideEnd[MXS];
void acAutomation(int root, char *s)
{
int p = root;
memset(hideEnd, -1, sizeof(hideEnd));
int sz = strlen(s);
FOR(i, 0, sz)
{
if (!isalpha(s[i]))
{
p = root;
continue;
}
int v = tolower(s[i]) - 'a';
while (node.trie[p][v] == -1 && p != root)
{
p = node.fail[p];
}
p = node.trie[p][v];
if (p == -1)
{
p = root;
}
int k = p;
while (k != root)
{
if (node.sz[k] > 0)
{
hideEnd[i - node.sz[k] + 1] = i;
}
else
{
break;
}
k = node.fail[k];
}
}
int tmp = -1;
FOR(i, 0, sz)
{
tmp = max(tmp, hideEnd[i]);
if (i<=tmp)
{
printf("*");
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
char s[MXS];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
node.init();
int root = node.newNode();
scanf("%d", &n);
FOR(i, 0, n)
{
scanf("%s", s);
len[i] = strlen(s);
insert(root, s, i);
}
bulidAC(root);
char ch;
while ((ch = getchar()) != '\n')
;
gets(s);
acAutomation(root, s);
}
}
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)