【HDU】病毒侵襲持續中(AC自動機+map),hdu自動機
一開始一直WA,之後發現這道題不止一組輸入,改成多組輸入之後就過了。
利用map把每個字串映射到它對應的結點上就行了。
| 11909467 |
2014-10-19 11:54:00 |
Accepted |
3065 |
234MS |
16912K |
2754 B |
G++ |
KinderRiven |
#include<queue>#include<map>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int maxn = 55555;const int max_size = 130;char ss[1111][55];map<string,int>vis;struct Trie{ int next[maxn][max_size]; int fail[maxn]; int val[maxn]; int sz; int root; int num[maxn]; int index(char e){ return e - 31; } void init(){ sz = 0; root = newnode(); } int newnode(){ val[sz] = 0; num[sz] = 0; memset(next[sz],-1,sizeof(next[sz])); sz ++; return sz - 1; } void insert(char *str){ int L = strlen(str); int u = root; for(int i = 0; i < L; i++){ int e = index(str[i]); if(next[u][e] == -1) next[u][e] = newnode(); u = next[u][e]; } val[u] = 1; //有單詞 vis[string(str)] = u; } void build(){ //建立失配邊 fail[root] = root; queue<int>q; for(int i = 0; i < max_size; 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 < max_size; 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 count(char *str){ int now = root; int L = strlen(str); for(int i = 0; i < L; i++){ int e = index(str[i]); now = next[now][e]; int temp = now; while(temp != root){ if(val[temp]){ //如果這個結點是個單詞末尾 num[temp] ++; //printf("%d\n",temp); } temp = fail[temp]; } } }};Trie ac;char _str[2222222];int main(){ int n; while(scanf("%d",&n) != EOF){ ac.init(); vis.clear(); getchar(); for(int i = 0; i < n ; i ++){ gets(ss[i]); ac.insert(ss[i]); } ac.build(); gets(_str); ac.count(_str); for(int i = 0; i < n; i++){ int t = vis[string(ss[i])]; if(ac.num[t]){ printf("%s: %d\n",ss[i],ac.num[t]); } } } return 0;}
杭電acm 3065解答
【題目分析】
AC自動機的題。如果不懂,可以去搜尋一下AC自動機的文章看看。題意不用多說,此題AC自動機模板題,考慮讀者已經掌握了AC自動機,可以切這個題來鞏固一下。注意是多case題目。
【AC原始碼】望採納哦~ 有問題歡迎再問哦~
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#define MAX 100005
#define inf 1499999999
using namespace std;
const int len=28;//最多隻有26個英文字母
struct Tril{
Tril *fail;
Tril *son[len];
int count;
Tril(){
fail=NULL;
count=0;
memset(son,NULL,sizeof(son));
}
};
char str[2000050];
char word[1005][55];
int ans[1005];
void creat(char *word,Tril *root,int num)///////////插入tril樹
{
Tril *p=root;
int lenth=strlen(word);
for(int i=0;i<lenth;i++)
{
int index=word[i]-'A';
if(p->son[index]==NULL)
p->son[index]=new Tril();
p=p->son[index];
}
p->count=num;
}
void creat_ac_automation(Tril *root)/////尋找失敗節點
{
queue<Tril *> q;
root->fail = NULL;
q.push(root);
while (!q.empty()) {
Tril * p_cur = q.front();
q.pop();
for (int i = 0; i < 26; ++i) {
if (p_cur->son[i] != NULL) {
if (p_cur == root) {
p_cur->son[i]->fail = root;
} else {
Tril * temp = p_cur->fail;
while (temp != NULL) {......餘下全文>>