ac自动机模板
2016-08-04 11:08
323 查看
指针版写法:
struct node
{
int val;
node *next[26];
node *fail;
node()
{
val = 0;
for(int i = 0; i < 26; i++)
next[i] = NULL;
fail = NULL;
}
};
node *root;
void insert(char *s)
{
int n = strlen(s);
node *curr = root;
for(int i = 0; i < n; i++)
{
int c = s[i] - 'a';
if(curr->next[c] == NULL)
{
node *newnode = new node;
curr->next[c] = newnode;
}
curr = curr->next[c];
}
curr->val++;
}
void build()
{
root->fail = NULL;
queue <node*> Q;
Q.push(root);
while(!Q.empty())
{
node *temp = Q.front();
node *p = NULL;
Q.pop();
for(int i = 0; i < 26; i++)
{
if(temp->next[i] == NULL)
continue;
if(temp == root)
temp->next[i]->fail = root;
else
{
p = temp->fail;
while(p != NULL)
{
if(p->next[i] != NULL)
{
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(p == NULL)
temp->next[i]->fail = root;
}
Q.push(temp->next[i]);
}
}
}
void find(char *s)
{
int n = strlen(s);
node *p = root;
for(int i = 0; i < n; i++)
{
int c = s[i] - 'a';
while(p != root && p->next[c] == NULL)
p = p->fail;
p = p->next[c];
if(p == NULL)
p = root;
node *temp = p;
while(temp != root)
{
ans += temp->val;
temp->val = 0;
temp = temp->fail;
}
}
}
void del(node *p)
{
for(int i = 0; i < 26; i++)
{
if(p->next[i] != NULL)
del(p->next[i]);
}
free(p);
}
数组版写法
const int maxnode = 500*210;
const int size = 128;
int ch[maxnode][size];
int val[maxnode];
int f[maxnode];
int sz;
bool id[maxn];
void init()
{
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
void insert(char *s, int v)
{
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++)
{
int c = s[i];
if(!ch[u][c])
{
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
}
void getFail()
{
queue <int> q;
f[0] = 0;
for(int c = 0; c < size; c++)
{
int u = ch[0][c];
if(u)
{
f[u] = 0;
q.push(u);
}
}
while(!q.empty())
{
int r = q.front(); q.pop();
for(int c = 0; c < size; c++)
{
int u = ch[r][c];
if(!u)
{
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
f[u] = ch[f[r]][c];
}
}
}
void find(char *s, int num)
{
int n = strlen(s);
int j = 0;
for(int i = 0; i < n; i++)
{
j = ch[j][s[i]];
int temp = j;
while(temp)
{
if(val[temp] > 0)
{
;
}
temp = f[temp];
}
}
}
struct node
{
int val;
node *next[26];
node *fail;
node()
{
val = 0;
for(int i = 0; i < 26; i++)
next[i] = NULL;
fail = NULL;
}
};
node *root;
void insert(char *s)
{
int n = strlen(s);
node *curr = root;
for(int i = 0; i < n; i++)
{
int c = s[i] - 'a';
if(curr->next[c] == NULL)
{
node *newnode = new node;
curr->next[c] = newnode;
}
curr = curr->next[c];
}
curr->val++;
}
void build()
{
root->fail = NULL;
queue <node*> Q;
Q.push(root);
while(!Q.empty())
{
node *temp = Q.front();
node *p = NULL;
Q.pop();
for(int i = 0; i < 26; i++)
{
if(temp->next[i] == NULL)
continue;
if(temp == root)
temp->next[i]->fail = root;
else
{
p = temp->fail;
while(p != NULL)
{
if(p->next[i] != NULL)
{
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(p == NULL)
temp->next[i]->fail = root;
}
Q.push(temp->next[i]);
}
}
}
void find(char *s)
{
int n = strlen(s);
node *p = root;
for(int i = 0; i < n; i++)
{
int c = s[i] - 'a';
while(p != root && p->next[c] == NULL)
p = p->fail;
p = p->next[c];
if(p == NULL)
p = root;
node *temp = p;
while(temp != root)
{
ans += temp->val;
temp->val = 0;
temp = temp->fail;
}
}
}
void del(node *p)
{
for(int i = 0; i < 26; i++)
{
if(p->next[i] != NULL)
del(p->next[i]);
}
free(p);
}
数组版写法
const int maxnode = 500*210;
const int size = 128;
int ch[maxnode][size];
int val[maxnode];
int f[maxnode];
int sz;
bool id[maxn];
void init()
{
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
void insert(char *s, int v)
{
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++)
{
int c = s[i];
if(!ch[u][c])
{
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
}
void getFail()
{
queue <int> q;
f[0] = 0;
for(int c = 0; c < size; c++)
{
int u = ch[0][c];
if(u)
{
f[u] = 0;
q.push(u);
}
}
while(!q.empty())
{
int r = q.front(); q.pop();
for(int c = 0; c < size; c++)
{
int u = ch[r][c];
if(!u)
{
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
f[u] = ch[f[r]][c];
}
}
}
void find(char *s, int num)
{
int n = strlen(s);
int j = 0;
for(int i = 0; i < n; i++)
{
j = ch[j][s[i]];
int temp = j;
while(temp)
{
if(val[temp] > 0)
{
;
}
temp = f[temp];
}
}
}
相关文章推荐
- 洛谷 P3796 【模板】AC自动机(加强版)
- AC自动机模板
- HDU2896,ac自动机模板题
- Keywords Search - HDU 2222(AC自动机模板)
- BZOJ 3172 [Tjoi2013] 单词 [AC自动机模板]
- HDU 2222 AC自动机模板
- HDU 2222 (AC自动机模板)
- AC自动机 模板与简单讲解 模板题:贴瓷砖
- HDOJ 2222 - Keywords Search 更新AC自动机模板...用类来表示
- 【模板练习——AC自动机】Keywords Search HDU - 2222
- AC自动机模板(数组+指针)hdu2222
- 洛谷P3796 【模板】AC自动机(加强版)
- hdu 2222 ac自动机模板题
- AC自动机模板【洛谷3796】
- AC自动机-HDU2222-模板题
- [洛谷3808]【模板】AC自动机(简单版)
- 【洛谷P3796】【模板】AC自动机(加强版)
- HDU 3065 AC自动机模板题
- 【洛谷】3808 【模板】AC自动机(简单版)
- 模板_AC自动机