hdu 3065 病毒侵襲持續中 AC自動機

2021-06-19 08:33:44 字數 1179 閱讀 4269

hdu 3065

題目大意:

給出n個模式串,最後給出主串

問有模式串在主串中出現的次數

解題思路:

ac自動機建立字典樹的用w值標記第幾個模式串

定義k值,匹配時若字典樹中的某個結點不等於k且w不為0則記錄該點

有多個主串需要匹配,所以不需要改變w的值,但可以判斷k的值

若該失敗指標的結點k值已經被匹配過則不需要再次匹配

**:

#include #include #include #define max 52

struct snodetree[max*1010];

int index,visit[1010],listb[1000001];

char ch[2000010],ch1[1010][max];

void insert(int star,int tlen,int k) //建立字典樹

else

s=tree[s].next[child];

}}void build_fail(int star) //建立失敗指標

now_fail=tree[now_fail].fail;

}if(now_fail==-1)

tree[tree[tempv].next[i]].fail=0;

listb[s++]=tree[tempv].next[i];}}

}}int ac_auto(int tlen) //bfs匹配

child=ch[i-1]-'a';

while(p!=0&&tree[p].next[child]==-1)

p=(tree[p].next[child]==-1)?0:tree[p].next[child];

temp=p;

while(temp!=0&&tree[temp].w!=-1) //記錄出現的次數

}return sum;

}int main()

build_fail(0); //建立失敗指標

getchar();

gets(ch); //主串可能有空格

k=ac_auto(strlen(ch)); //匹配

for(i=1;i<=n;i++)}}

return 0;

}

hdu 3065 病毒侵襲持續中

題目大意及思路 ac自動機。include include include include include include include include include include includeusing namespace std define inf 0x3f3f3f3f define ...

HDU 3065 病毒侵襲持續中

這就更簡單了,都不用把out標記成false了 題目中的病毒都是大寫字母這個條件應該怎麼用?include include include include include include include include include include include include include ...

HDU 3065 病毒侵襲持續中

詢問每個模式串在文字傳中出現的次數。文字串中出現的字元不一定都是大寫字母,只需要在匹配的時候,對文字串進行特殊處理,將連續的大寫字母段當成合法的乙個文字串即可。然後 就是簡單的統計了。include include include include include include using name...