【HDU 2243 考研路茫茫——单词情结】 AC自动机+DP+矩阵快速幂

HDU2243
在做本题之前推荐做POJ2778
题解
POJ2778求的是用给定字符集构造出的长度为n的字符串中没出现过给定字符串的字符串有多少个
本题统计的是出现过的,那么我们只需要算出一共可能的种数,再算出长度为1-n可能的出现过给定字符串的字符串个数,相减就是答案。
首先,我们先计算一共可能的种数,
f [ i ] = 26 1 + 26 2 + . . . . .26 i
所以 f [ i ] = 26 f [ i 1 ] + 26
所以我们可以利用矩阵快速幂求出一共可能的种数sum,注意这里要用unsigned long long
之后我们利用上题的做法求出转移矩阵,设转移矩阵为 T ,那么 T n 的第一行的和即为长度为n的串的转移方案而如果我们想求出所有长度的方案数之和,我们就需要在转移矩阵上加一维,用来存储上一个状态第一行之和,这样最后 T n 的第一行之和就是长度为1-n所有方案之和sum2(慢慢理解,想一下矩阵快速幂
最后用sum-sum2即是最终结果,注意所有变量都应该用unsigned long long。而且这里不能直接输出sum-sum2会出现负数,应该用sum-=sum2才能实现自动溢出。
HDU2243代码

//由于本题存在两个大小不同的矩阵,所以开了两个struct和两个分别矩阵快速幂函数
//但是如果用类去构造应该能清晰一些。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define  dbg(x) cout<<#x<<" "<<x<<endl
typedef unsigned long long ll;
const int maxn = 2e6+5;
map<char,int> mp;
struct ACTrie
{
    int tree[maxn][27],fail[maxn];
    int end_[maxn];
    int root,num,cnt;
    int newnode()
    {
        for(int i=0;i<26;i++)
            tree[cnt][i]=-1;
        end_[cnt]=0;
        return cnt++;
    }
    void init()
    {
        cnt=0;
        num=0;
        root=newnode();
    }
    void insert_(char str[])
    {
        int pos=root;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            int id=str[i]-'a';
            if(tree[pos][id]==-1) tree[pos][id]=newnode();
            pos=tree[pos][id];
        }
        end_[pos]=1;
    }
    void build()
    {
        queue<int> que;
        fail[root]=root;
        for(int i=0;i<26;i++)
        {
            if(tree[root][i]==-1) tree[root][i]=root;
            else
            {
                fail[tree[root][i]]=root;
                que.push(tree[root][i]);
            }
        }
        while(!que.empty())
        {
            int now=que.front();
            que.pop();
            for(int i=0;i<26;i++)
            {
                if(tree[now][i]==-1) tree[now][i]=tree[fail[now]][i];
                else
                {
                    fail[tree[now][i]]=tree[fail[now]][i];
                    que.push(tree[now][i]);
                }
                end_[tree[now][i]]|=end_[tree[fail[now]][i]];
            }
        }
    }
};
ACTrie ac;
struct mat
{
    ll jz[110][110];
};
mat make_mat()
{
    mat res;
    memset(res.jz,0,sizeof(res.jz));
    for(int i=0;i<ac.cnt;i++)
    {
        if(ac.end_[i]) continue;
        for(int j=0;j<26;j++)
        {
            if(ac.end_[ac.tree[i][j]]) continue;
            ++res.jz[i][ac.tree[i][j]];
        }
    }
    for(int i=0;i<=ac.cnt;i++)//在最后一列加上1,实现当前行和的转移
    {
        res.jz[i][ac.cnt]=1;
    }
    return res;
}
mat mat_mul(mat x,mat y)
{
    mat res;
    memset(res.jz,0,sizeof(res.jz));
    for(int i=0;i<=ac.cnt;i++)
        for(int j=0;j<=ac.cnt;j++)
            for(int k=0;k<=ac.cnt;k++)
                res.jz[i][j]=res.jz[i][j]+x.jz[i][k]*y.jz[k][j];
        return res;
}
ll power_mod (ll b)//.res是系数矩阵,ans是变换矩阵左->ans,右->res.
{
    mat ans,res;
    res=make_mat();
    memset(ans.jz,0,sizeof(ans.jz));
    for(int i=0;i<=ac.cnt;i++)
        ans.jz[i][i]=1;
    while(b>0)
    {
        if(b&1)  ans=mat_mul(res,ans);//所以应该系数矩阵在前ans,res);
        b=b>>1;
        res=mat_mul(res,res);
    }
    ll tmp=0;
    for(int i=0;i<=ac.cnt;i++)
        tmp=tmp+ans.jz[0][i];
    return tmp;//返回指定位置元素
}
char str[11];
struct mat2
{
    ll jz[2][2];
};
mat2 mat_mul2(mat2 x,mat2 y)
{
    mat2 res;
    memset(res.jz,0,sizeof(res.jz));
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                res.jz[i][j]=res.jz[i][j]+x.jz[i][k]*y.jz[k][j];
        return res;
}
ll power_mod2(ll b)//.res是系数矩阵,ans是变换矩阵左->ans,右->res.
{
    mat2 ans,res;
    res.jz[0][0]=26;
    res.jz[0][1]=1;
    res.jz[1][0]=0;
    res.jz[1][1]=1;//根据要求设定系数单位矩阵,
    ans.jz[0][0]=0;
    ans.jz[1][0]=26;//初始化ans矩阵,只需要初始化每行的第一列
    while(b>0)
    {
        if(b&1)  ans=mat_mul2(res,ans);//所以应该系数矩阵在前ans,res);
        b=b>>1;
        res=mat_mul2(res,res);
        //cout<<ans.jz[0][0]<<endl;
    }
    return ans.jz[0][0];//返回指定位置元素
}
int main()
{
    int n;
    ll m;
    while(scanf("%d%I64d",&n,&m)!=EOF)
    {
        ac.init();
        while(n--)
        {
            scanf("%s",str);
            ac.insert_(str);
        }
        ac.build();
        ll tmp=power_mod2(m)+1;//加上长度为0的串
        ll res=power_mod(m);
        tmp-=res;//注意是-=不是直接输出
        cout<<tmp<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38891827/article/details/80684155
今日推荐