HDU_3065 病毒侵袭持续中

http://acm.hdu.edu.cn/showproblem.php?pid=3065

目标串中各个模式串出现了几次 用num数组保存次数 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);


#define Nn 500007
#define Mc 13
#define N 1007

using namespace std;
int num[N];

class Acautomaton
{
    private:

    int chd[Nn][Mc];
    int fail[Nn];
    int val[Nn];
    int ID[Mc];
    int Q[Nn];
    int sz;

    public :

    void Init()
    {
        fail[0] = 0;
        int i;
        for(i=0;i<Mc;i++)
        {
            ID[i]=i;
        }
    }
    void Reset()
    {
        CL(chd[0],0);
        sz = 1;
    }
    void Insert(char *s,int key)
    {
        int k;
        int p = 0;
        for (; *s; s++)
        {
            k = ID[*s-97];
//            printf("%d\n",k);
            if (!chd[p][k])
            {
                CL(chd[sz],0);
                val[sz] = 0;
                chd[p][k] = sz++;
            }
            p = chd[p][k];
        }
        val[p] = key;
    }
    void Build()
    {
        int *s = Q, *e = Q;
        int i;
        for (i = 0; i < Mc; ++i)
        {
            if (chd[0][i])
            {
                fail[chd[0][i]] = 0;
                *e++ = chd[0][i];
            }
        }

        while (s != e)
        {
//            printf(">>>\n");
            int u = *s++;
            for (i = 0; i < Mc; ++i)
            {
                int &v = chd[u][i];
                if (v)
                {
                    *e++ = v;
                    fail[v] = chd[fail[u]][i];
                    val[v] |= val[fail[v]];
                }
                else v = chd[fail[u]][i];
            }
        }
    }
    void solve(char *s)
    {
        int k;
        int p = 0;
        for (; *s; s++)
        {
            k = ID[*s-97];
            while (!chd[p][k] && p != 0) p = fail[p];

            p = chd[p][k];
            int rt = p;
            while (rt != 0&&val[rt] != 0)
            {
                num[val[rt]]++; 这里val不能置为-1,如果置为-1就不能统计某一个字符串出现的次数了,只能表示出新过
                rt = fail[rt];
            }
        }
    }

}ac;

int n,m;
char s[N][51], s2[2000007];

int main()
{
//    printf("%d\n",'z');
//    Read();
    int i;
    while (~scanf("%d",&n))
    {
        CL(num,0);
        ac.Reset(); ac.Init();
        for (i = 0; i < n; ++i)
        {
            scanf("%s",s[i]);
            ac.Insert(s[i],i + 1);
        }
        ac.Build();
        scanf("%s",s2);
        ac.solve(s2);
        for(i=0;i<n;i++)
        {
            if(num[i+1]) printf("%s: %d\n",s[i],num[i+1]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值