hdu2825——ac自动机,DP

题意:求长度为n,至少包含了给定字符串k个的字符串有多少种。

还是多段图递推的思想,不过这一次是在ac自动机上,用10个二进制位表示已经出现的字符串。

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

const int maxn = 1000 + 10;
const int mod = 20090717;
int n, m, k;

int bit_count(int num) { return num == 0 ? 0 : (num & 1) + bit_count(num / 2); }

struct ac_automaton
{
    int ch[maxn][30], val[maxn];
    int f[maxn], last[maxn], sz;
    int dp[105][27][(1 << 10) + 10];

    void init()
    {
        sz = 1; memset(ch, 0, sizeof(ch));
        memset(val, 0, sizeof(val)); memset(f, 0, sizeof(f));
        memset(last, 0, sizeof(last));
        memset(dp, 0, sizeof(dp));
    }

    void insert(char * s, int v)
    {
        int u = 0;
        for(int i = 0; s[i]; ++i)
        {
            int c = s[i] - 'a';
            if(!ch[u][c])
            {
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] |= (1 << v);
    }

    void getFail()
    {
        queue<int> q; f[0] = 0;
        for(int c = 0; c < 26; ++c)
        {
            int u = ch[0][c];
            if(u) { f[u] = 0; q.push(u); last[u] = 0; }
            else ch[0][c] = 0;
        }
        while(!q.empty())
        {
            int r = q.front(); q.pop();
            for(int c = 0; c < 26; ++c)
            {
                int u = ch[r][c];
                if(u)
                {
                    q.push(u);
                    int p = f[r];
                    while(p && ch[p][c] == 0) p = f[p];
                    f[u] = ch[p][c]; last[u] = val[f[u]] ? f[u] : last[f[u]];
                    val[u] |= val[last[u]];
                }
                else ch[r][c] = ch[f[r]][c];
            }
        }
    }

    int slove()
    {
        dp[0][0][0] = 1;
        for(int l = 0; l < n; ++l)
        {
            for(int u = 0; u < sz; ++u)
            {
                for(int s = 0; s < (1 << m); ++s)
                {
                    if(dp[u][l][s] == 0) continue;
                    for(int c = 0; c < 26; ++c)
                    {
                        int v = ch[u][c];
                        dp[v][l + 1][s | val[v]] += dp[u][l][s];
                        dp[v][l + 1][s | val[v]] %= mod;
                    }
                }
            }
        }
        int res = 0;
        for(int i = 0; i < sz; ++i)
        {
            for(int j = 0; j < (1 << m); ++j)
            {
                if(bit_count(j) >= k)
                {
                    res += dp[i][n][j];
                    res %= mod;
                }
            }
        }
        return res;
    }
}a;

int main()
{
    freopen("in", "r", stdin);
    while(~scanf("%d %d %d", &n, &m, &k))
    {
        if(n == 0 && m == 0 && k == 0) break;
        a.init();
        for(int i = 0; i < m; ++i)
        {
            char str[100];
            scanf("%s", str);
            a.insert(str, i);
        }
        a.getFail();
        printf("%d\n", a.slove());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值