码迷,mamicode.com
首页 > 其他好文 > 详细

AC自动机

时间:2014-05-14 23:43:19      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   tar   

ZOJ 3430 Detect the Virus

挺扯的一个题,解码有点问题+注意用int存,跟HDU2222差不多...

bubuko.com,布布扣
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 60010
int trie[N][260];
int que[100000];
int fail[100000];
int hash[1001];
int o[100001];
int str[100000];
int t,num;
void CL()
{
    memset(trie,-1,sizeof(trie));
    memset(o,0,sizeof(o));
    t = 1;
}
int fun(char x)
{
    if(x >= A&&x <= Z)
        return x-A;
    else if(x >= a&&x <= z)
        return x-a+26;
    else if(x >= 0&&x <= 9)
        return x-0+52;
    else if(x == +)
        return 62;
    else
        return 63;
}
void judge(char *s)
{
    int i,j,len,temp,n;
    len = strlen(s);
    n = 0;
    for(i = 0; i < len; i ++)
    {
        if(s[i] == =)
        {
            n -= 2;//开始这里没理解对。
            continue;
        }
        temp = fun(s[i]);
        for(j = 5; j >= 0; j --)
        {
            if(temp&(1<<j))
                que[n++] = 1;
            else
                que[n++] = 0;
        }
    }
    for(i = 0; i < n;)
    {
        str[i/8] = 0;
        for(j = 7; j >= 0; j --)
        {
            if(que[i])
                str[i/8] += 1<<j;
            i ++;
        }
    }
    num = n/8;
}
void insert(int x)
{
    int i,len,root;
    len = num;
    root = 0;
    for(i = 0; i < len; i ++)
    {
        if(trie[root][str[i]] == -1)
        {
            trie[root][str[i]] = t ++;
        }
        root = trie[root][str[i]];
    }
    o[root] = x;
}
void build_ac()
{
    int head,tail,front,i;
    head = tail = 0;
    for(i = 0; i < 260; i ++)
    {
        if(trie[0][i] != -1)
        {
            fail[trie[0][i]] = 0;
            que[tail++] = trie[0][i];
        }
        else
        {
            trie[0][i] = 0;
        }
    }
    while(head != tail)
    {
        front = que[head++];
        for(i = 0; i < 260; i ++)
        {
            if(trie[front][i] != -1)
            {
                que[tail++] = trie[front][i];
                fail[trie[front][i]] = trie[fail[front]][i];
            }
            else
            {
                trie[front][i] = trie[fail[front]][i];
            }
        }
    }
}
void query()
{
    int len,key,temp,i,root;
    root = 0;
    len = num;
    for(i = 0; i < len; i ++)
    {
        temp = str[i];
        root = trie[root][temp];
        key = root;
        while(key != 0)
        {
            hash[o[key]] = 1;
            key = fail[key];
        }
    }
}
int main()
{
    int n,i,m,j;
    char ch[5000];
    while(scanf("%d",&n)!=EOF)
    {
        CL();
        for(i = 1; i <= n; i ++)
        {
            scanf("%s",ch);
            judge(ch);
            insert(i);
        }
        build_ac();
        scanf("%d",&m);
        for(i = 0; i < m; i ++)
        {
            memset(hash,0,sizeof(hash));
            scanf("%s",ch);
            judge(ch);
            query();
            int ans = 0;
            for(j = 1; j <= n; j ++)
            {
                if(hash[j])
                    ans ++;
            }
            printf("%d\n",ans);
        }
        printf("\n");
    }
    return 0;
}
View Code

 

 

AC自动机,布布扣,bubuko.com

AC自动机

标签:style   blog   class   code   c   tar   

原文地址:http://www.cnblogs.com/naix-x/p/3728752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!