poj 2778(ac自动机+矩阵快速幂)

题意:给出n个字符串(由字母A C G T组成),然后问一个长度为m的字符串(由字母A C G T组成)中不出现n个字符串中任意一个的种类是多少。
题解:因为m的范围是20000000000,要用到矩阵快速幂加速运算,邻接矩阵mat[i][j]表示从节点i到节点j走一步有多少种走法,那么mat[i][j]^n是n步可达矩阵,也就是从节点i到节点j走n步有多少种走法,这个步骤用矩阵快速幂,可以根据trie图中到节点next[i][j]是否是一个模式串确定mat[i][next[i][j]]的两个节点是否相邻,注意一个串的后缀如果是模式串那么这个串也不能算作可达。

#include 
#include 
#include 
#include 
using namespace std;
const int MOD = 100000;
const int N = 105;
const int SIGMA_SIZE = 4;
int Next[N][SIGMA_SIZE], val[N], fail[N], sz, mp[256];
char str[N];
struct Mat {
    long long a[N][N];
}ori, res;

Mat multiply(const Mat &x, const Mat &y) {
    Mat temp;
    for (int i = 0; i < sz; i++)
        for (int j = 0; j < sz; j++) {
            temp.a[i][j] = 0;
            for (int k = 0; k < sz; k++)
                temp.a[i][j] += x.a[i][k] * y.a[k][j];
            temp.a[i][j] %= MOD;
        }
    return temp;
}

void calc(int m) {
    while (m) {
        if (m & 1)
            res = multiply(res, ori);
        m >>= 1;
        ori = multiply(ori, ori);
    }
}

void init() {
    sz = 1;
    memset(Next[0], 0, sizeof(Next[0]));
    val[0] = 0;
}

void insert(char *s) {
    int u = 0, len = strlen(s);
    for (int i = 0; i < len; i++) {
        int k = mp[s[i]];
        if (!Next[u][k]) {
            memset(Next[sz], 0, sizeof(Next[sz]));
            val[sz] = 0;
            Next[u][k] = sz++;
        }
        u = Next[u][k];
    }
    val[u] = 1;
}

void getFail() {
    queue<int> Q;
    fail[0] = 0;
    for (int i = 0; i < SIGMA_SIZE; i++) {
        if (Next[0][i]) {
            fail[Next[0][i]] = 0;
            Q.push(Next[0][i]);
        }
    }
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        if (val[fail[u]]) //注意这里,一旦u的后缀是模式串,u也不能出现
            val[u] = 1;
        for (int i = 0; i < SIGMA_SIZE; i++) {
            if (!Next[u][i])
                Next[u][i] = Next[fail[u]][i];
            else {
                fail[Next[u][i]] = Next[fail[u]][i];
                Q.push(Next[u][i]);
            }
        }
    }
}

int main() {
    mp['A'] = 0;
    mp['C'] = 1;
    mp['G'] = 2;
    mp['T'] = 3;
    int n, m;
    while (scanf("%d%d", &n, &m) == 2) {
        init();
        memset(ori.a, 0, sizeof(ori.a));
        for (int i = 0; i < n; i++) {
            scanf("%s", str);
            insert(str);
        }
        getFail();
        for (int i = 0; i < sz; i++)
            for (int j = 0; j < sz; j++)
                res.a[i][j] = 0;
        for (int i = 0; i < sz; i++)
            res.a[i][i] = 1;
        for (int i = 0; i < sz; i++)
            for (int j = 0; j < SIGMA_SIZE; j++)
                if (!val[i] && !val[Next[i][j]])
                    ori.a[i][Next[i][j]]++;
        calc(m);
        int ans = 0;
        for (int i = 0; i < sz; i++)
            ans += res.a[0][i];
        printf("%d\n", ans % MOD);
    }
    return 0;
}

你可能感兴趣的:(ACM-AC自动机,ACM-矩阵快速幂)