具體題目見HDU2222,其實就是一個裸的多串匹配的問題(給出一個主串和N個子串,求出幾個子串在主串中出現過)。
我真是太沙茶了……這么水的題目調了N久,找了N位神犇幫我看代碼,最終才找出來BUG……
易疵點:
(1)本題的子串是可以相同的,此時Trie的每個結點要設一個mul值,表示該結點對應的字符串在所有子串中重復的次數,另外,不要為了省空間把mul定義成char型,有可能所有的字符串全相同,因此需要定義成int(事實證明不會爆空間),這是本沙茶被折磨了這么久的主要原因;
(2)Trie采用靜態存儲,0號結點作為空結點(NULL),因此真正的結點編號從1開始,另外root一般都是1號結點;
(3)注意在建立自動機以及匹配的時候,所有要沿fail上溯的地方,其邊界都是0(NULL,注意不是root)或者找到一個有對應子結點的結點。注意到0還沒有找到的處理方法:在建立自動機的時候,將T[j]置為root;在匹配的時候,將x置為root;
代碼(模板)(那些標了Attention的地方都是易疵的):
【2011年10月19日】今天發現了匹配過程中的一個可優化的地方:對于一個點x以及它的所有返回結點(這里把所有沿著x的失敗指針不斷上溯直到root路徑上的結點都稱為返回結點),由于不可重復計數,可以將它們的mul值置為原來mul值的相反數(-mul),而不是0,表示該結點已經統計過。這樣在下一次y的上溯過程中一旦發現一個mul值為負的點就不用繼續上溯了,因為上面的點一定也已經統計過了。
當然,這僅限于單主串,如果是多主串則需要在每次匹配之前把Trie樹中所有結點的mul值(如果是負數的的話)全部重新取反。為了節省時間,可以在匹配過程中把所有統計過的(mul值改為負數的)結點全部放進一個輔助的隊列里,然后取反時只要處理隊列中的結點就行了。
加入該優化后的代碼(solve部分):
下面是優化的實測結果(第一個為優化后的,第二個為優化前的),可以看出,該優化的力度很大。
我真是太沙茶了……這么水的題目調了N久,找了N位神犇幫我看代碼,最終才找出來BUG……
易疵點:
(1)本題的子串是可以相同的,此時Trie的每個結點要設一個mul值,表示該結點對應的字符串在所有子串中重復的次數,另外,不要為了省空間把mul定義成char型,有可能所有的字符串全相同,因此需要定義成int(事實證明不會爆空間),這是本沙茶被折磨了這么久的主要原因;
(2)Trie采用靜態存儲,0號結點作為空結點(NULL),因此真正的結點編號從1開始,另外root一般都是1號結點;
(3)注意在建立自動機以及匹配的時候,所有要沿fail上溯的地方,其邊界都是0(NULL,注意不是root)或者找到一個有對應子結點的結點。注意到0還沒有找到的處理方法:在建立自動機的時候,將T[j]置為root;在匹配的時候,將x置為root;
代碼(模板)(那些標了Attention的地方都是易疵的):
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
using std::string;
#define re(i, n) for (int i=0; i<n; i++)
#define root 1
const int MAXN = 500001, MAXLEN = 1000001, SZ = 26, INF = ~0U >> 2;
struct node {
int mul, ch[SZ], fail; //Attention
} T[MAXN];
int N, Q[MAXN], res;
string s0, A;
char tmp[MAXLEN], tmp0[51];
void ins()
{
int len = s0.length(), x = root, c;
re(i, len) {
c = s0[i] - 97;
if (!T[x].ch[c]) {T[x].ch[c] = ++N; T[N].mul = 0; re(j, SZ) T[N].ch[j] = 0;}
x = T[x].ch[c];
}
T[x].mul++;
}
void mkf()
{
Q[0] = root; T[root].fail = 0;
int i, j, x;
for (int front=0, rear=0; front<=rear; front++) {
i = Q[front];
re(k, SZ) if (j = T[i].ch[k]) {
x = T[i].fail;
while (x && !T[x].ch[k]) x = T[x].fail; //Attention
if (x) T[j].fail = T[x].ch[k]; else T[j].fail = root; //Attention
Q[++rear] = j;
}
}
}
void solve()
{
int len = A.length(), x = root, y, c; res = 0;
re(i, len) {
c = A[i] - 97;
while (x && !T[x].ch[c]) x = T[x].fail; //Attention
if (!x) x = root; else x = T[x].ch[c]; //Attention
y = x;
while (y) {res += T[y].mul; T[y].mul = 0; y = T[y].fail;} //Attention
}
}
int main()
{
int tests, n;
scanf("%d", &tests);
re(testno, tests) {
N = 1; T[root].mul = 0; re(i, SZ) T[root].ch[i] = 0;
scanf("%d", &n); getchar();
re(i, n) {
gets(tmp0);
s0 = tmp0;
ins();
}
gets(tmp);
A = tmp;
mkf();
solve();
printf("%d\n", res);
}
return 0;
}
#include <stdio.h>
#include <string>
using namespace std;
using std::string;
#define re(i, n) for (int i=0; i<n; i++)
#define root 1
const int MAXN = 500001, MAXLEN = 1000001, SZ = 26, INF = ~0U >> 2;
struct node {
int mul, ch[SZ], fail; //Attention
} T[MAXN];
int N, Q[MAXN], res;
string s0, A;
char tmp[MAXLEN], tmp0[51];
void ins()
{
int len = s0.length(), x = root, c;
re(i, len) {
c = s0[i] - 97;
if (!T[x].ch[c]) {T[x].ch[c] = ++N; T[N].mul = 0; re(j, SZ) T[N].ch[j] = 0;}
x = T[x].ch[c];
}
T[x].mul++;
}
void mkf()
{
Q[0] = root; T[root].fail = 0;
int i, j, x;
for (int front=0, rear=0; front<=rear; front++) {
i = Q[front];
re(k, SZ) if (j = T[i].ch[k]) {
x = T[i].fail;
while (x && !T[x].ch[k]) x = T[x].fail; //Attention
if (x) T[j].fail = T[x].ch[k]; else T[j].fail = root; //Attention
Q[++rear] = j;
}
}
}
void solve()
{
int len = A.length(), x = root, y, c; res = 0;
re(i, len) {
c = A[i] - 97;
while (x && !T[x].ch[c]) x = T[x].fail; //Attention
if (!x) x = root; else x = T[x].ch[c]; //Attention
y = x;
while (y) {res += T[y].mul; T[y].mul = 0; y = T[y].fail;} //Attention
}
}
int main()
{
int tests, n;
scanf("%d", &tests);
re(testno, tests) {
N = 1; T[root].mul = 0; re(i, SZ) T[root].ch[i] = 0;
scanf("%d", &n); getchar();
re(i, n) {
gets(tmp0);
s0 = tmp0;
ins();
}
gets(tmp);
A = tmp;
mkf();
solve();
printf("%d\n", res);
}
return 0;
}
【2011年10月19日】今天發現了匹配過程中的一個可優化的地方:對于一個點x以及它的所有返回結點(這里把所有沿著x的失敗指針不斷上溯直到root路徑上的結點都稱為返回結點),由于不可重復計數,可以將它們的mul值置為原來mul值的相反數(-mul),而不是0,表示該結點已經統計過。這樣在下一次y的上溯過程中一旦發現一個mul值為負的點就不用繼續上溯了,因為上面的點一定也已經統計過了。
當然,這僅限于單主串,如果是多主串則需要在每次匹配之前把Trie樹中所有結點的mul值(如果是負數的的話)全部重新取反。為了節省時間,可以在匹配過程中把所有統計過的(mul值改為負數的)結點全部放進一個輔助的隊列里,然后取反時只要處理隊列中的結點就行了。
加入該優化后的代碼(solve部分):
void solve()
{
int len = A.length(), x = root, y, c; res = 0;
re(i, len) {
c = A[i] - 97;
while (x && !T[x].ch[c]) x = T[x].fail;
if (!x) x = root; else x = T[x].ch[c];
y = x;
while (y && T[y].mul >= 0) {res += T[y].mul; T[y].mul = -T[y].mul; y = T[y].fail;}
}
}
{
int len = A.length(), x = root, y, c; res = 0;
re(i, len) {
c = A[i] - 97;
while (x && !T[x].ch[c]) x = T[x].fail;
if (!x) x = root; else x = T[x].ch[c];
y = x;
while (y && T[y].mul >= 0) {res += T[y].mul; T[y].mul = -T[y].mul; y = T[y].fail;}
}
}
下面是優化的實測結果(第一個為優化后的,第二個為優化前的),可以看出,該優化的力度很大。
