【POJ 4052】Hrinity(AC自动机)

【POJ 4052】Hrinity(AC自动机)

题目是PDF版,地址在:http://poj.org/problem?id=4044

题目大意:多模式串匹配,给出的是经过压缩的串,解压即可。
问文本串有几个模式串能跟他匹配。如果有多个互相包含的模式串,只记录最外层的。

因为说解压前后总长度不超过 5105 ,所以全部建出个AC自动机。

然后先用文本串匹配,把匹配到的标记。然后对于每个被标记的模式串,跑一遍,把子串去掉标记。

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
const int maxn = 5123456;

struct Node
{
    int next[26],fail,cnt;
} nd[maxn];

bool vis[6666];
int pos[6666];
int id[6666];
char str[maxn];
char ps[maxn];
int len,tp;

int NewNode()
{
    memset(nd[tp].next,0,sizeof(nd[tp].next));
    nd[tp].fail = nd[tp].cnt = 0;
    return tp++;
}

void Add(int root,int c)
{
    int k;
    while(str[len])
    {
        k = str[len]-'A';
        if(!nd[root].next[k]) nd[root].next[k] = NewNode();
        root = nd[root].next[k];
        len++;
    }

    if(!nd[root].cnt) id[c] = nd[root].cnt = c;
    else id[c] = nd[root].cnt;
}

void Build_Fail(int root)
{
    queue <int> q;

    q.push(root);
    int f;

    while(!q.empty())
    {
        root = q.front();
        //printf("%d %d\n",root,nd[root].fail);
        q.pop();
        for(int i = 0; i < 26; ++i)
        {
            if(!nd[root].next[i]) continue;
            if(root) 
            {
                f = nd[root].fail;
                while(f && !nd[f].next[i]) f = nd[f].fail;
                nd[nd[root].next[i]].fail = nd[f].next[i]? nd[f].next[i]: f;
            }
            q.push(nd[root].next[i]);
        }

    }
}

int Search(int root,char *str,int len,int id,int f)
{

    //for(int i = 0; i < len; ++i)
    //  putchar(str[i]);
    //puts("");

    int ans = 0,k;
    for(int i = 0; i < len; ++i)
    {
        k = str[i]-'A';
        while(root && !nd[root].next[k]) root = nd[root].fail;

        if(nd[root].next[k]) root = nd[root].next[k];
        else continue;

        if(f) vis[nd[root].cnt] = f;
        else
        {
            for(int j = root; j; j = nd[j].fail)
            {
                if(nd[j].cnt != id) vis[nd[j].cnt] = f;
            }
        }
    }
    return ans;
}

bool IsChar(char ch)
{
    return 'A' <= ch && ch <= 'Z';
}

void read(char *str)
{
    char ch;
    int pos = 0;

    while((ch = getchar()) && ch == '\n' || ch == EOF || ch == '\r' || ch == ' ');

    while(ch && ch != '\n' && ch != EOF && ch != '\r')
    {
        if(IsChar(ch))
        {
            str[pos++] = ch;
            ch = getchar();
            continue;
        }

        int cnt = 0;
        ch = getchar();
        while(!IsChar(ch))
        {
            cnt = cnt*10+ch-'0';
            ch = getchar();
        }

        while(cnt--) str[pos++] = ch;
        ch = getchar();
        ch = getchar();
    }
    str[pos] = 0;
}

int main()
{
    //fread("");
    //fwrite("");

    int t,n;

    scanf("%d",&t);

    while(t--)
    {
        scanf("%d",&n);

        len = 0;
        tp = 0;
        int root = NewNode();

        for(int i = 1; i <= n; ++i)
        {
            pos[i] = len;
            read(str+len);
            Add(root,i);
        }
        pos[n+1] = len;

        read(ps);

        Build_Fail(root);
        //puts(ps);
        //puts(str);

        memset(vis,0,sizeof(vis));
        Search(root,ps,strlen(ps),0,1);

        for(int i = 1; i <= n; ++i)
        {
            //printf("%d %d\n",id[i],vis[i]);
            if(id[i] == i && vis[i]) Search(root,str+pos[i],pos[i+1]-pos[i],id[i],0);
        }

        int ans = 0;
        for(int i = 1; i <= n; ++i)
            ans += vis[i];

        printf("%d\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值