給m個子串,求長度為n的不包含子串的母串數,最直接的應該是暴搜,肯定tle,考慮用ac自動機
將子串建成字典樹,通過next表來構造矩陣,然後用矩陣快速冪求長度為n的數量
鄰接矩陣
對於a(i,j)^k 是指從i到j經過k個點的所有情況數
注意對於end陣列,如果某個節點如果fail指標end陣列為1,那麼end【該節點】也是1
string要開全域性變數,不然不能執行= =
#include#includeview code#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair#define c 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 100000
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using
namespace
std;
const
double g=10.0,eps=1e-7
;const
int n=100+10,maxn=100+10,inf=0x3f3f3f
;struct
node;
node mul(node x,node y)
node quick_mul(node x,
intn)
return
ans;
}struct
trie
intnewnode()
void
init()
void insertstring(string
s)
end[now]=1
; }
void
build()
}while(!q.empty())}}
}int solve(int
n)
};trie ac;
string
s;int
main()
ac.build();
cout
}return0;
}/********************
*******************
*/
poj2778 ac自動機 矩陣快速冪
poj2778 求長度為m且不包含n個子串的種類數.參考自這個部落格.ac自動機 矩陣快速冪.這兒有個結論.給定乙個有向圖,問從a點恰好走k步 允許重複經過邊 到達b點的方案數mod p的值 把給定的圖轉為鄰接矩陣,即a i,j 1當且僅當存在一條邊i j。令c a a,那麼c i,j a i,k ...
poj2778(AC自動機 矩陣快速冪)
題意 給你n個字串,問你長度為m的字串且字串中不含有那n個子串的字串的數量 解題思路 這道題一開始就不太懂,還以為是組合數學的題目,後面看了別人的部落格,才知道這是屬於ac自動機的另一種用法,是關於fail陣列的運用,因為題目問的是不允許包含那n個字串,所以我們可以這麼想,假設乙個trie樹每個結點...
poj 2778 AC自動機 DP 矩陣快速冪
include include include includeusing namespace std typedef int64 type const int kind 4 每個節點的子節點的個數上限 const int mod 100000 const int size 109 轉移矩陣的行大小 ...