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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法

發布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://acm.hdu.edu.cn/showproblem.php?pid=2243

這是一題AC自動機 + 矩陣快速冪的題目,

首先知道總答案應該是26^1 + 26^2 + 26^3 .... + 26^L,用等比數列的前n項和是無法做的,因為出現小數。

這個可以直接看到F[n] = 26 * F[n - 1] + 26,然后矩陣快速冪即可。

然后需要減去那些一個詞根都不包含的單詞的總數,這個可以用AC自動機算出來。就是至少包含一個詞根的答案。

?

現在關鍵就是求,長度小于等于L的通路總數。

我們知道通路等于L的通路總數,正是一個可達矩陣e[i][j]的L次冪。

那么小于等于L的,也就是e + e^2 + e^3 + e^4 + ... + e^L

這是無法求的。

做法是新增一個節點,然后每一個節點都和這個新的節點連上一條邊。

?

假設本來的可達矩陣是

e[1][1] = 1, e[1][2] = 1, e[1][3] = 1

e[2][1] = 0, e[2][2] = 0, e[2][3] = 1;

e[3][1] = 0, e[3][2] = 0, e[3][3] = 0;

那么長度是2的通路數就是,e^2,然后得到了這個圖。

?

所以長度是2的通路數,是4。e[1][1] ---> e[1][1],e[1][1]--->e[1][2]。e[1][1] ---> e[1][3],e[1][2]--->e[2][3]

那么長度是1的通路數就不能統計了,就是e[1][1]、e[1][2]、e[1][3]丟失了。

?

那么我們新增一個節點,使得其他本來的節點都和它連一條邊。

然后算e^2的時候,

就會把e[1][1]記錄到e[1][1]-->e[1][4]中,所以記錄成功。、

然后e[1][4]本來是不存在的,所以這條路徑是多余的,

#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> typedef unsigned long long int ULL; const int N = 26; struct node {int flag;int id;struct node *Fail; //失敗指針,匹配失敗,跳去最大前后綴struct node *pNext[N]; } tree[100 * 20]; int t; //字典樹的節點 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 = str[i] - 'a';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]->Failstruct 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 ; }const int maxn = 30 + 3; struct Matrix {ULL a[maxn][maxn];int row;int col; }; //應對稀疏矩陣,更快。 struct Matrix matrix_mul(struct Matrix a, struct Matrix b) { //求解矩陣a*b%MODstruct Matrix c = {0}; //這個要多次用到,棧分配問題,maxn不能開太大,//LL的時候更加是,空間是maxn*maxn的,這樣時間用得很多,4和5相差300msc.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];}}}}return c; } struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, int n) { //求解a*b^n%MODwhile (n) {if (n & 1) {ans = matrix_mul(ans, base);//傳數組不能亂傳,不滿足交換律 }n >>= 1;base = matrix_mul(base, base);}return ans; }int n, L; char str[222]; void work() {t = 1;struct node *T = NULL;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 + 1;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]++;}}t++;for (int i = 1; i <= t; ++i) {e.a[i][t] = 1;}Matrix I = {0};I.row = I.col = t;for (int i = 1; i <= t; ++i) {I.a[i][i] = 1;}Matrix res = quick_matrix_pow(I, e, L);I.row = 1, I.col = 2;I.a[1][1] = 0, I.a[1][2] = 1;e.row = e.col = 2;e.a[1][1] = 26, e.a[1][2] = 0;e.a[2][1] = 26, e.a[2][2] = 1;Matrix res2 = quick_matrix_pow(I, e, L);ULL ans = res2.a[1][1]; // cout << ans << endl;for (int i = 1; i <= t; ++i) {ans -= res.a[1][i];}ans++; //減了一個多余的路徑cout << ans << endl; }int main() { #ifdef localfreopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endifwhile (scanf("%d%d", &n, &L) > 0) work();return 0; } View Code

?

轉載于:https://www.cnblogs.com/liuweimingcprogram/p/6490034.html

總結

以上是生活随笔為你收集整理的HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。