日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU -2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

發(fā)布時(shí)間:2024/4/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU -2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

思路

假設(shè)讓求長(zhǎng)度為LLL且不包含詞根的個(gè)數(shù),對(duì)所有的詞根建acacac自動(dòng)機(jī),然后用矩陣MMM表示可轉(zhuǎn)移狀態(tài),最后最快速冪即可。
如何求長(zhǎng)度不超過LLL且不包含LLL且不包含詞根的個(gè)數(shù),可以利用這個(gè)矩陣[M101]\begin{gathered} \begin{bmatrix} M & 1 \\ 0 & 1 \end{bmatrix} \end{gathered}[M0?11?]?,這樣就可以得到∑i=0nMi\sum\limits_{i=0}^{n}M^ii=0n?Mi,最后用總的狀態(tài)數(shù)[26101]n\begin{bmatrix} 26 & 1 \\ 0 & 1\end{bmatrix} ^ n[260?11?]n減去即可

#include <vector> #include <queue> #include <stdio.h> #include <iostream> const int maxn = 1e5 + 5; const int inf = 0x3f3f3f3f; using namespace std; struct Matrix{int n;unsigned long long mat[151][151];Matrix(){}Matrix(int _n) {n = _n;for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {mat[i][j] = 0;}}}Matrix operator *(const Matrix &b) const{Matrix ans = Matrix(n);for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {for (int k = 0; k < n; ++k) {ans.mat[i][j] += mat[i][k] * b.mat[k][j];// ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * b.mat[k][j] % mod) % mod;}}}return ans;} }; struct Trie{int nex[maxn][26], fail[maxn], end[maxn];int root, p;inline int newnode() {for (int i = 0; i < 26; ++i) {nex[p][i] = -1;}end[p++] = 0;return p - 1;}inline void init() {p = 0;root = newnode();}inline void insert(char *buf) {int now = root;for (int i = 0; buf[i]; ++i) {if (nex[now][buf[i]-'a'] == -1) nex[now][buf[i]-'a'] = newnode();now = nex[now][buf[i]-'a'];}end[now]++;} inline void build() {queue<int> que;fail[root] = root;for (int i = 0; i < 26; ++i) {if (nex[root][i] == -1)nex[root][i] = root;else {fail[nex[root][i]] = root;que.push(nex[root][i]);}}while (!que.empty()) {int now = que.front();que.pop();if (end[fail[now]]) end[now] = 1; for (int i = 0; i < 26; ++i) {if (nex[now][i] == -1) nex[now][i] = nex[fail[now]][i];else {fail[nex[now][i]] = nex[fail[now]][i];que.push(nex[now][i]);}}}}inline Matrix get() {Matrix tmp = Matrix(p*2);for (int i = 0; i < p; ++i) {for (int j = 0; j < 26; ++j) {if (end[i] || end[nex[i][j]]) continue;tmp.mat[i][nex[i][j]]++;}}for (int i = 0; i < p; ++i) tmp.mat[i][i+p] = tmp.mat[i+p][i+p] = 1;return tmp;} }ac; Matrix Pow(Matrix a, int n) {Matrix ans = Matrix(a.n);Matrix tmp = a;for (int i = 0; i < a.n; ++i) {ans.mat[i][i] = 1;}while (n) {if (n & 1) ans = ans * tmp;tmp = tmp * tmp;n >>= 1;}return ans; } char s[maxn]; int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, m;while (scanf("%d %d\n", &n, &m) != EOF) {ac.init();for (int i = 0; i < n; ++i) scanf("%s", s), ac.insert(s);ac.build();Matrix ans = ac.get();ans = Pow(ans, m);Matrix tmp = Matrix(2);tmp.mat[0][0] = 26;tmp.mat[0][1] = 1;tmp.mat[1][1] = 1;tmp = Pow(tmp, m);unsigned long long sum = tmp.mat[0][0] + tmp.mat[0][1] - 1;for (int j = 0; j < 2*ac.p; ++j) sum -= ans.mat[0][j];printf("%llu\n", sum+1);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的HDU -2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。