POJ 2778 DNA Sequence AC自动机 + 矩阵快速幂

http://poj.org/problem?id=2778

首先将病毒串假如去AC自动机上。然后构造trie图后。

题目是要构造一个长度为len的,不包含那些病毒的串,的种类数。

转化题目,就是要求在root点上,走len步,能有多少种情况不走到病毒串上。

那么,对于每一个AC自动机上的节点,我们都知道其遍历4个方向后,能去到哪一个状态,就是转移的时候,能转移到去哪一个节点。

不过如果它是病毒节点或者它的等价态是病毒节点的话,那就不能转移了。

然后构造去一个可达矩阵e

e[i][j]表示在i号节点,走一步,能走到j号节点的方法数,那么e^len就是所求。

记得特判病毒串是0的时候。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int n, len;
const int N = 4;
struct node {
    int flag;
    int id;
    struct node *Fail;    //失败指针,匹配失败,跳去最大前后缀
    struct node *pNext[N];
} tree[10 * 20];
int t;     //字典树的节点
int getid(char ch) {
    if (ch == 'A') return 0;
    else if (ch == 'T') return 1;
    else if (ch == 'C') return 2;
    else return 3;
}
struct node *create() {   //其实也只是清空数据而已,多case有用
    struct node *p = &tree[t++];
    p->flag = 0;
    p->Fail = NULL;
    p->id = t - 1;
    for (int i = 0; i < N; i++) {
        p->pNext[i] = NULL;
    }
    return p;
}
void toinsert(struct node **T, char str[]) {
    struct node *p = *T;
    if (p == NULL) {
        p = *T = create();
    }
    for (int i = 1; str[i]; i++) {
        int id = getid(str[i]);
        if (p->pNext[id] == NULL) {
            p->pNext[id] = create();
        }
        p = p->pNext[id];
    }
    p->flag++;    //相同的单词算两次
    return ;
}
void BuiltFail(struct node **T) {
    //根节点没有失败指针,所以都是需要特判的
    //思路就是去到爸爸的失败指针那里,找东西匹配,这样是最优的
    struct node *p = *T; //用个p去代替修改
    struct node *root = *T;
    if (p == NULL) return ;
    //树上bfs,要更改的是p->pNext[i]->Fail
    struct node *que[t + 20]; //这里的t是节点总数,字典树那里统计的,要用G++编译
    int head = 0, tail = 0;
    que[tail++] = root;
    while (head < tail) {
        p = que[head]; //p取出第一个元素 ★
        for (int i = 0; i < N; i++) { //看看存不存在这个节点
            if (p->pNext[i] != NULL) { //存在的才需要管失败指针。
                if (p == root) { //如果爸爸是根节点的话
                    p->pNext[i]->Fail = root; //指向根节点
                } else {
                    struct node *FailNode = p->Fail; //首先找到爸爸的失败指针
                    while (FailNode != NULL) {
                        if (FailNode->pNext[i] != NULL) { //存在
                            p->pNext[i]->Fail = FailNode->pNext[i];
                            if (FailNode->pNext[i]->flag) {
                                p->pNext[i]->flag = 1;
                            }
                            break;
                        }
                        FailNode = FailNode->Fail; //回溯
                    }
                    if (FailNode == NULL) { //如果还是空,那么就指向根算了
                        p->pNext[i]->Fail = root;
                    }
                }
                que[tail++] = p->pNext[i]; //这个id是存在的,入队bfs
            } else if (p == root) {  //变化问题,使得不存在的边也建立起来。
                p->pNext[i] = root;
            } else {
                p->pNext[i] = p->Fail->pNext[i]; //变化到LCP。可以快速匹配到病毒。
            }
        }
        head++;
    }
    return ;
}
char str[222];
const int maxn = 100 + 3;
struct Matrix {
    LL a[maxn][maxn];
    int row;
    int col;
};
//应对稀疏矩阵,更快。
struct Matrix matrix_mul(struct Matrix a, struct Matrix b, int MOD) { //求解矩阵a*b%MOD
    struct Matrix c = {0};  //这个要多次用到,栈分配问题,maxn不能开太大,
    //LL的时候更加是,空间是maxn*maxn的,这样时间用得很多,4和5相差300ms
    c.row = a.row; //行等于第一个矩阵的行
    c.col = b.col; //列等于第二个矩阵的列
    for (int i = 1; i <= a.row; ++i) {
        for (int k = 1; k <= a.col; ++k) {
            if (a.a[i][k]) { //应付稀疏矩阵,0就不用枚举下面了
                for (int j = 1; j <= b.col; ++j) {
                    c.a[i][j] += a.a[i][k] * b.a[k][j];
                    c.a[i][j] = (c.a[i][j] + MOD) % MOD; //负数取模
                }
            }
        }
    }
    return c;
}
struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, int n, int MOD) {
//求解a*b^n%MOD
    while (n) {
        if (n & 1) {
            ans = matrix_mul(ans, base, MOD);//传数组不能乱传,不满足交换律
        }
        n >>= 1;
        base = matrix_mul(base, base, MOD);
    }
    return ans;
}
const int MOD = 100000;
void work() {
    t = 1;
    struct node *T = NULL;
    scanf("%d%d", &n, &len);
    for (int i = 1; i <= n; ++i) {
        scanf("%s", str + 1);
        toinsert(&T, str);
    }
    BuiltFail(&T);
    t--;
    Matrix e = {0};
    e.row = e.col = t;
    for (int i = 1; i <= t; ++i) {
        if (tree[i].flag) continue;
        int id1 = tree[i].id;
        for (int j = 0; j < N; ++j) {
            if (tree[i].pNext[j]->flag) continue;
            int id2 = tree[i].pNext[j]->id;
            e.a[id1][id2]++;
        }
    }
//    for (int i = 1; i <= e.row; ++i) {
//        for (int j = 1; j <= e.col; ++j) {
//            cout << e.a[i][j] << " ";
//        }
//        cout << endl;
//    }
    Matrix I = {0};
    I.row = I.col = t;
    for (int i = 1; i <= t; ++i) {
        I.a[i][i] = 1;
    }
    if (n == 0) {
        e.row = e.col = 1;
        e.a[1][1] = 4;
        I.row = I.col = 1;
        I.a[1][1] = 1;
        t = 1;
    }
    Matrix res = quick_matrix_pow(I, e, len, MOD);
    int ans = 0;
    for (int i = 1; i <= t; ++i) {
        ans += res.a[1][i];
        ans %= MOD;
    }
    cout << ans << endl;
//    for (int i = 1; i <= t; ++i) {
//        for (int j = 1; j <= t; ++j) {
//            cout << res.a[i][j] << " ";
//        }
//        cout << endl;
//    }
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6483730.html