【MZ】hdu 2243 AC自动机 + 矩阵加速

problem:

给你 n 个病毒,问长度为 [1, m] 的,包括病毒的有多少。全都是小写字母。


think:

和 poj 2778 有点像。不一样的是:

1)  这个是求包括的,我这么做的,加一维用来表示已经包括了病毒的状态。

2)  求长度是 [1, m] 的。在矩阵里面维度变成二倍。

[A 1]
[1 0] 左下面的 1 就是最后所求。A是矩阵的话,就把 1 变成 I 矩阵, 把 0 变成 零矩阵。

这里建立好的转移矩阵左下面的是 I 矩阵, 所以ans矩阵初始化的时候不是 I 矩阵,而是转移矩阵。

如果把 ans 矩阵初始化为大 I 矩阵,那么矩阵乘 (m+1) 次就好了。


code:

const int NN = 33;
const int kind = 26;

int cnt;//状态数,矩阵大小
char ch[9];

int fail[NN];//每个状态的fail指针
int child[NN][kind];//01两种
int end[NN];//每个状态是否已经包含一整个病毒
LL dp[NN][NN];//转移矩阵

struct tri{
    LL a[NN<<1][NN<<1];
};

int newNode(){
    ++cnt;
    for(int i=0; i<kind; ++i) child[cnt][i] = -1;
    end[cnt] = 0;
    return cnt;
}

void insert(char *str, int root){
    int p = root;
    for(int i=0; str[i]; ++i){
        int k = str[i] - 'a';
        if(child[p][k] == -1) child[p][k] = newNode();
        p = child[p][k];
    }
    end[p] = 1;
}

//这个建立fail指针不同于hdu2222
//这里的fail不能有空指针,必须要指向一个状态
queue<int>q;
void build_fail(int root){
    while(!q.empty()) q.pop();
    fail[root] = root;
    q.push(root);
    while(!q.empty()){
        int tmp = q.front();
        q.pop();
        if(end[fail[tmp]]==1) end[tmp] = 1;//tmp也已经包括病毒
        for(int k = 0; k < kind; ++k){
            if(child[tmp][k]!=-1){
                if(tmp==root) fail[child[tmp][k]] = root;
                else fail[child[tmp][k]] = child[fail[tmp]][k];
                q.push(child[tmp][k]);
            } else {
                if(tmp==root) child[root][k] = root;
                else child[tmp][k] = child[fail[tmp]][k];
            }
        }
    }
}

void init_dp(){
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i <= cnt; ++i){
        if(end[i]==1){
            dp[i][cnt+1] = 26;
            continue;
        }
        for(int j = 0; j < kind; ++j){
            int tmp = child[i][j];
            if(end[tmp]==0) ++dp[i][tmp];
            else ++dp[i][cnt+1];
        }
    }
    dp[cnt+1][cnt+1] = 26;
    ++cnt;
}

tri mul(tri x, tri y){
    tri z;
    for(int i = 0; i <= cnt; ++i){
        for(int j = 0; j <= cnt; ++j){
            z.a[i][j] = 0;
            for(int k = 0; k <= cnt; ++k){
                z.a[i][j] += x.a[i][k] * y.a[k][j];
            }
        }
    }
    return z;
}

LL solve(int m){
    if(m==0) return 0;
    tri ans, tmp;
    // 这里的ans不是I矩阵,是因为dp左下方的就是一个小的I矩阵
    for(int i = 0; i <= cnt; ++i){
        for(int j = 0; j <= cnt; ++j){
            ans.a[i][j] = tmp.a[i][j] = dp[i][j];
            ans.a[i][j+cnt+1] = tmp.a[i][j+cnt+1] = 0;
            ans.a[i+cnt+1][j] = tmp.a[i+cnt+1][j] = i==j;
            ans.a[i+cnt+1][j+cnt+1] = tmp.a[i+cnt+1][j+cnt+1] = i==j;
        }
    }
    cnt = cnt * 2 + 1;
    while(m){
        if(m&1) ans = mul(ans, tmp);
        tmp = mul(tmp, tmp);
        m >>= 1;
    }

    LL res = ans.a[cnt/2+1][cnt/2];
    return res;
}

int main(){
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF){
        cnt = -1;
        int root = newNode();

        for(int i = 0; i < n; ++i){
            scanf("%s", ch);
            insert(ch, root);
        }
        build_fail(root);
        init_dp();
        LL res = solve(m);
        cout<<res<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值