hdu 2243(ac自动机+矩阵快速幂)

题意:有n个小写字母组成的模式串,问长度不超过L的小写字母串中至少出现一个模式串的种类是多少。
题解:这道题和poj 2778类似,不过是把长度小于L的串的可能情况也计入,把邻接矩阵多一维存总和,然后结果用总种类数减一个模式串也不出现的种类数。总种类数很大,26^1 + 26^2 + 26^3 + … + 26^n,也用矩阵快速幂计算。f(n) = 26 * f(n - 1) + 26。
初始矩阵:
| f(n - 1) 1 |
| 0 0 |
系数矩阵:
| 26 0 |
| 26 1 |
相乘得到:
| f(n) 1 |
| 0 0 |

#include 
#include 
#include 
#include 
#include 
#define ll unsigned long long
using namespace std;
const int N = 55;
const int SIGMA_SIZE = 26;
struct Mat {
    ll a[N][N];
}ori, res;
int Next[N][SIGMA_SIZE], fail[N], val[N], sz, n, L;
char str[N];

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 = s[i] - 'a';
        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]])
            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]);
            }
        }
    }
}

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];
        }
    return temp;
}

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

int main() {
    while (scanf("%d%d", &n, &L) == 2) {
        init();
        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] = ori.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[Next[i][j]])
                    ori.a[i][Next[i][j]]++;
        for (int i = 0; i <= sz; i++)
            ori.a[i][sz] = 1;
        calc(L);
        ll ans = 0;
        for (int i = 0; i <= sz; i++)
            ans += res.a[0][i];
        ori.a[0][0] = ori.a[1][0] = 26;
        ori.a[0][1] = 0;
        ori.a[1][1] = 1;
        res.a[0][1] = 1;
        res.a[0][0] = res.a[1][1] = res.a[1][0] = 0;
        sz = 1;
        calc(L);
        ll ans2 = res.a[0][0];
        printf("%llu\n", ans2 - ans + 1);
    }
    return 0;
}

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