ZOJ 2243 考研路茫茫――单词情结 AC自动机+矩阵快速幂

就是问你长度小于等于L的单词且出现过至少一个子串的数量。首先算出长度小于等于L的单词有多少种,26^1 + 26^2 + 26^3+ …+26^L,然后再算出长度为1,2,3…L的单词中包含子串的数量,相减就行,第二部分用AC自动机求出矩阵,然后矩阵快速幂,其中从bin神那里学到一个技巧,矩阵的1次+2次+3次+…L次幂的和等于在原矩阵上最右边多加一列从0到L+1的1

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define LL unsigned long long
const int maxnode=50;
const int maxn=40;
const int sigma_size=26;
char s[15];
struct Node {
    int son[sigma_size];
    int val,fail;
}ch[maxnode];

struct Matrix {
    LL m[maxn][maxn];
    Matrix() {memset(m,0,sizeof(m));}
}mat,n26;
int sz=1;
struct AC {
    queue<int>Q;
    void init(int x) {ch[x].fail=ch[x].val=0;memset(ch[x].son,0,sizeof(ch[x].son));}
    int idx(char c) {return c-'a';}

    void insert(char s[],int v) {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++) {
            int c=idx(s[i]);
            if(!ch[u].son[c]) {
                init(sz);
                ch[u].son[c]=sz++;
            }
            u=ch[u].son[c];
        }
        ch[u].val=v;
    }

    void build() {
        for(int i=0;i<26;i++) if(ch[0].son[i]) Q.push(ch[0].son[i]);
        while(!Q.empty()) {
            int now=Q.front();Q.pop();
            int fail=ch[now].fail;
            for(int i=0;i<26;i++) {
                int nxt=ch[now].son[i];
                if(nxt) {
                    ch[nxt].fail=ch[fail].son[i];
                    Q.push(nxt);
                }
                else ch[now].son[i]=ch[fail].son[i];
                ch[ch[now].son[i]].val|=ch[ch[ch[now].fail].son[i]].val;
            }
        }
    }

    int solve(char s[]) {
        int len=strlen(s),ans=0,now=0;
        for(int i=0;i<len;i++) {
            int c=idx(s[i]);
            while(now&&!ch[now].son[c]) now=ch[now].fail;
            now=ch[now].son[c];
            int p=now;
            while(p&&ch[p].val!=-1) {
                ans+=ch[p].val;
                p=ch[p].fail;
            }
        }
        return ans;
    }

    void buildMatrix() {
        memset(mat.m,0,sizeof(mat.m));
        for(int i=0;i<sz;i++) {
            for(int j=0;j<26;j++) {
                if(ch[i].val!=1&&ch[ch[i].son[j]].val!=1) {
                    mat.m[i][ch[i].son[j]]++;
                }
            }
        }
        for(int i=0;i<sz+1;i++) mat.m[i][sz]=1;
    }
}ans;

Matrix operator*(const Matrix &m1,const Matrix &m2){
    Matrix m;
    for(int i=0; i<=sz; ++i){
        for(int j=0; j<=sz; ++j){
            for(int k=0; k<=sz; ++k){
                m.m[i][j]+=m1.m[i][k]*m2.m[k][j];
            }
        }
    }
    return m;
}

Matrix q_pow(Matrix x,LL n) {
    Matrix res;
    for(int i=0;i<=sz;i++) res.m[i][i]=1;
    while(n) {
        if(n%2==1) res=res*x;
        n/=2;
        x=x*x;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    LL n,m;
    while(cin>>n>>m) {
        memset(n26.m,0,sizeof(n26.m));
        n26.m[0][0]=26,n26.m[0][1]=n26.m[1][1]=1;
        n26=q_pow(n26,m);
        ans.init(0);sz=1;
        for(int i=1;i<=n;i++) {
            cin>>s;
            ans.insert(s,1);
        }
        ans.build();
        ans.buildMatrix();
        mat=q_pow(mat,m);
        LL res=n26.m[0][0]+n26.m[0][1];
        for(int i=0;i<=sz;i++) {
            res=res-mat.m[0][i];
        }
        cout<<res<<endl;
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值