HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
http://acm.hdu.edu.cn/showproblem.php?pid=2243
這是一題AC自動機(jī) + 矩陣快速冪的題目,
首先知道總答案應(yīng)該是26^1 + 26^2 + 26^3 .... + 26^L,用等比數(shù)列的前n項和是無法做的,因為出現(xiàn)小數(shù)。
這個可以直接看到F[n] = 26 * F[n - 1] + 26,然后矩陣快速冪即可。
然后需要減去那些一個詞根都不包含的單詞的總數(shù),這個可以用AC自動機(jī)算出來。就是至少包含一個詞根的答案。
?
現(xiàn)在關(guān)鍵就是求,長度小于等于L的通路總數(shù)。
我們知道通路等于L的通路總數(shù),正是一個可達(dá)矩陣e[i][j]的L次冪。
那么小于等于L的,也就是e + e^2 + e^3 + e^4 + ... + e^L
這是無法求的。
做法是新增一個節(jié)點,然后每一個節(jié)點都和這個新的節(jié)點連上一條邊。
?
假設(shè)本來的可達(dá)矩陣是
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的通路數(shù)就是,e^2,然后得到了這個圖。
?
所以長度是2的通路數(shù),是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的通路數(shù)就不能統(tǒng)計了,就是e[1][1]、e[1][2]、e[1][3]丟失了。
?
那么我們新增一個節(jié)點,使得其他本來的節(jié)點都和它連一條邊。
然后算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; //字典樹的節(jié)點 struct node *create() { //其實也只是清空數(shù)據(jù)而已,多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) {//根節(jié)點沒有失敗指針,所以都是需要特判的//思路就是去到爸爸的失敗指針那里,找東西匹配,這樣是最優(yōu)的struct node *p = *T; //用個p去代替修改struct node *root = *T;if (p == NULL) return ;//樹上bfs,要更改的是p->pNext[i]->Failstruct node *que[t + 20]; //這里的t是節(jié)點總數(shù),字典樹那里統(tǒng)計的,要用G++編譯int head = 0, tail = 0;que[tail++] = root;while (head < tail) {p = que[head]; //p取出第一個元素 ★for (int i = 0; i < N; i++) { //看看存不存在這個節(jié)點if (p->pNext[i] != NULL) { //存在的才需要管失敗指針。if (p == root) { //如果爸爸是根節(jié)點的話p->pNext[i]->Fail = root; //指向根節(jié)點} 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; }; //應(yīng)對稀疏矩陣,更快。 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]) { //應(yīng)付稀疏矩陣,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);//傳數(shù)組不能亂傳,不滿足交換律 }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?
轉(zhuǎn)載于:https://www.cnblogs.com/liuweimingcprogram/p/6490034.html
總結(jié)
以上是生活随笔為你收集整理的HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java笔试题大全_java笔试题大全带
- 下一篇: Android 6.0及以上版本动态申请