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

来源:互联网 发布:康师傅免流量软件 编辑:程序博客网 时间:2024/06/02 17:46

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;}


原创粉丝点击