hdu 2243 考研路茫茫——单词情结

13 篇文章 0 订阅
2 篇文章 0 订阅

矩阵快速幂、幂相加是重点

还设计到一些ac自动机的dp

#include <CSTDIO>
#include <STRING>
#include <IOSTREAM>
using namespace std;

typedef unsigned long long ULL;
const int MAXN = 60;
const int szGP = MAXN*MAXN*(sizeof (ULL));
struct tire
{
    int next[26];
    int fail;
    int flg;
    void init()
    {
        memset(next, 0, sizeof next);
        fail = flg = 0;
    }
} TREE[MAXN];
int root, index, q[40];
void inst(char *ch)
{
    int a = root;
    for (;*ch; ch++)
    {
        if (TREE[a].next[*ch-'a'] == 0)
        {
            TREE[++index].init();
            TREE[a].next[*ch-'a'] = index;
        }
        a = TREE[a].next[*ch-'a'];
    }
    TREE[a].flg = 1;
}
void buidAC()
{
    int head = 0, tail = 0;
    q[head++] = root;
    int i, tp;
    while (head != tail)
    {
        tp = q[tail++];
        TREE[tp].flg |= TREE[TREE[tp].fail].flg;
        for (i = 0; i< 26; i++)
        {
            if (TREE[tp].next[i])
            {
                if (tp)
                    TREE[TREE[tp].next[i]].fail = TREE[TREE[tp].fail].next[i];
                q[head++] = TREE[tp].next[i];
            }
            else
            {
                TREE[tp].next[i] = TREE[TREE[tp].fail].next[i];
            }
        }
    }
}
struct Matrx
{
    ULL data[MAXN][MAXN];
    int mSize;
    Matrx(){ mSize = index+1; }
    Matrx(int n) 
    {
        mSize = n;
        memset(data, 0, szGP);
    }
    Matrx operator + (const Matrx & a) const
    {
        Matrx re(mSize);
        for (int i = 0; i< mSize; ++i)
        {
            for (int j = 0; j< mSize; ++j)
            {
                re.data[i][j] = data[i][j] + a.data[i][j];
            }
        }
        return re;
    }
    Matrx operator * (const Matrx &a) const
    {
        Matrx tp(mSize);
        int i, j, k;
        for (i = 0; i< mSize; ++i)
        {
            for (j = 0; j< mSize; ++j)
            {
                for (k = 0; k< mSize; ++k)
                {
                    tp.data[i][j] += data[i][k]*a.data[k][j];
                }
            }
        }
        return tp;
    }
    Matrx pow(int p)
    {
        Matrx res = *this, tp = *this;
        --p;
        while (p)
        {
            if (p & 0x1)
                res = res*tp;
            tp = tp*tp;
            p>>=1;
        }
        return res;
    }
};
Matrx gp;
void getDPtmp()    // 长度为1的状态转移矩阵
{
    int i, j;
    memset(gp.data, 0, szGP);
    gp.mSize = index+1;
    for ( i = 0; i<= index; i++)
    {
        if (TREE[i].flg)
            continue;
        for (j = 0; j<26; j++)
        {
            if (!TREE[TREE[i].next[j]].flg)
                ++gp.data[i][TREE[i].next[j]];
        }
    }
}

int main()   
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int n, l;
    int i, j;
    char tpcha[10];
    while (scanf("%d%d", &n, &l) == 2)
    {
        index =root = 0;
        TREE[0].init();
        for (i = 0; i< n; ++i)
        {
            scanf("%s", tpcha);
            inst(tpcha);
        }
        buidAC();
        getDPtmp();
        Matrx as(2);
        as.data[0][0] = 26;as.data[0][1] = 0;
        as.data[1][0] = 1;as.data[1][1] = 1;
        Matrx bs = as.pow(l);
        ULL res = bs.data[0][0] + bs.data[1][0] - 1;
        Matrx cs(2*index+2);
        for (i = 0; i<=index; ++i)
        {
            for (j = 0; j<= index; ++j)
            {
                cs.data[i][j] = gp.data[i][j];
                cs.data[i][j+index+1] = 0;
            }
            cs.data[i+index+1][i] = 1;
            cs.data[i+index+1][i+index+1] = 1;
        }
        Matrx ds = cs.pow(l);
        ULL cnt = 0;
        for (i = 0; i<= index; ++i)
        {
            cnt = cnt + ds.data[0][i] + ds.data[index+1][i];
        }
        --cnt;
        res-=cnt;
        cout<<res<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值