HDOJ-3065 病毒侵袭持续中

这题是多例。。。

#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
using namespace std;

const int maxn = 30000;
int Node[maxn][3], link[maxn], fail[maxn];
char elem[maxn];
int cnt[1010];
char pt[1010][60];
int top;
queue <int> q;
int newNode(char ch)
{
    top ++;
    link[top] = Node[top][0] = Node[top][1] = Node[top][2] = 0;
    elem[top] = ch;
    return top;
}

void Insert(char str[], int id)
{
    int p = 1, idx = 0;
    while(str[idx])
    {
        if(!Node[p][2]) Node[p][2] = newNode(str[idx]);
        p = Node[p][2];
        while(elem[p] != str[idx])
        {
            if(str[idx] < elem[p])
            {
                if(!Node[p][0]) Node[p][0] = newNode(str[idx]);
                p = Node[p][0];
            }
            else
            {
                if(!Node[p][1]) Node[p][1] = newNode(str[idx]);
                p = Node[p][1];
            }
        }
        idx ++;
    }
    link[p] = id;
}

int findFail(int p, int fp)
{
    while(elem[p] != elem[fp])
    {
        if(elem[p] < elem[fp]) 
            if(!Node[fp][0]) return 0;
            else fp = Node[fp][0];
        else 
            if(!Node[fp][1]) return 0;
            else fp = Node[fp][1];
    }
    fail[p] = fp;
    return 1;
}
int que[27], tl, hd;
void travelPre(int p, int pre)
{
    tl = hd = 0;
    que[++hd] = p;
    while(tl != hd)
    {
        p = que[++tl];
        q.push(p);
        int tmp = fail[pre];
        while(tmp && !findFail(p, Node[tmp][2])) tmp = fail[tmp];
        if(tmp == 0) fail[p] = 1;
        if(Node[p][0]) que[++hd] = Node[p][0];
        if(Node[p][1]) que[++hd] = Node[p][1];
    }
}
void getFail()
{
    while(!q.empty()) q.pop();
    q.push(1);
    int p;
    fail[1] = 0;
    while(!q.empty())
    {
        p = q.front();
        q.pop();
        if(Node[p][2])
            travelPre(Node[p][2], p);
    }
    
}
char ss[2000010];
int matchCH(int p, char ch, int &pre)
{
    while(ch != elem[p])
    {
        if(ch < elem[p])
            if(!Node[p][0])return 0;
            else p = Node[p][0];
        else
            if(!Node[p][1]) return 0;
            else p = Node[p][1];
    }
    pre = p;
    while(p)
    {
        if(link[p])
            cnt[link[p]] ++;
        p = fail[p];
    }
    return 1;
}
void match()
{
    int idx = 0, p = 1;
    while(ss[idx])
    {
        while(p && !matchCH(Node[p][2], ss[idx], p)) p = fail[p];
        if(p == 0) p = 1;
        idx ++;
    }
}
int main()
{
    int n, i;
    while(~scanf("%d",&n))
    {
    top = 0;
    newNode('\0');
    for(i = 1; i <= n; i++)
    {
        scanf("%s", pt[i]);
        Insert(pt[i], i);
        cnt[i] = 0;
    }
    getFail();
    scanf("%s", ss);
    match();
    for(i = 1; i <= n; i++)
        if(cnt[i])
            printf("%s: %d\n", pt[i], cnt[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值