題目大意:給出一個不能出現的字符串,問長度為k的字符串有多少種。
思路:用給定串建立一個AC自動機(或者KMP隨便了),然後跑矩陣乘法就行了。
CODE:
#include#include #include #include #include using namespace std; int k,length,p; char s[MAX]; int son[MAX][MAX],cnt; bool end[MAX]; void Insert() { int now = 0; for(int i = 1; i <= length; ++i) { if(!son[now][s[i] - '0']) son[now][s[i] - '0'] = ++cnt; now = now->son[now][s[i] - '0']; } end[now] = true; } void MakeTrieGraph() { static queue q; for(int i = 0; i <= 9; ++i) if(son[0][i]) q.push(i); while(!q.empty()) { int x = q.front(); q.pop(); for(int y,i = 0; i <= 9; ++i) { if(y = son[x][i]) { int r; for(r = fail[x]; r && !son[r][i]; r = fail[r]); faik[j] = son[r][i]; q.push(y); } else son[x][i] = son[fail[x]][i]; } } } int main() { cin >> k >> length >> p; scanf("%s",s); Insert(); MakeTrieGraph(); for(int i = 0; i <= cnt; ++i) for(int j = 0; i <= 9; ++i) if(son[i][j] && !end[i] && !end[son[i][j]]) ++Ma return 0; }