1. 程式人生 > >2017多校賽 1002 Rikka with String(AC自動機+狀壓)

2017多校賽 1002 Rikka with String(AC自動機+狀壓)

這個題目和之前做的一道題目很像:類似的題目
對於題目要求列舉到2L長度的串,由於01串本身的性質我們只用列舉到L就行了,那一半是對稱的。在列舉這一半時是可以隨便列舉的,因為這一般確定了,那麼另一半也確定了,這樣2L的串一定是個01串。
然後對於輸入的n個串做如下處理:對於每個輸入的串講其插入AC自動機裡面,然後將每個串的反串(然後01顛倒)也插入進去。比如對於001,我們不僅要插入001,還要將011插入,因為當在前L中出現011,由於01串的性質在,L到2L中一定會出現001。
同時還有特殊情況要額外處理,對於001來說,如果前面L的串中沒有出現001,但是在L的最後兩位出現00了,根據01串的性質,在2L的串中一定會出先001,所以對每個輸入的字串中具有像00這樣性質的串也要匯入AC自動機中,但是要把狀態壓縮到AC自動機的另一個數組中,因為這是列舉到最後一位才會處理的,和之前的狀態不能放在一起


處理方法:
對於一個輸入的串,我們去列舉串的每一個位置,在這個位置將串一分為2,如果這個串在對稱範圍內滿足01串的性質,那麼我們就把長的那個插入自動機中

做好以上工作後就,在自動機上跑一遍狀壓DP就可以了,和之前那道題目一樣的做法。dp[i][j][k],表示當列舉到i這個位置時,位於AC自動機的j節點上,當前的狀態壓縮在k上。列舉到最後一位別忘了特殊處理哦!

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; #define Max_N 2000 #define MOD 998244353 struct Trie { int next[Max_N][3], fail[Max_N], end1[Max_N],end2[Max_N]; int root, L; int newnode() { for (int i = 0; i < 2; i++) next[L][i] = -1; end1[L++] = 0; return L-1; } void
init() { L = 0; root = newnode(); memset(end2, 0, sizeof(end2)); } void insert(char buf[], int x, int flag) { int len = strlen(buf); int now = root; for (int i = 0; i < len; i++) { if (next[now][buf[i]-'0'] == -1) next[now][buf[i]-'0'] = newnode(); now = next[now][buf[i] - '0']; } if (flag) end1[now] |= 1<<x; else end2[now] |= 1<<x; } void build() { queue<int> q; fail[root] = root; for (int i = 0; i < 2; i++) if (next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; q.push(next[root][i]); } while (!q.empty()) { int now = q.front(); q.pop(); end1[now] |= end1[fail[now]]; end2[now] |= end2[fail[now]]; for (int i = 0; i < 2; i++) if (next[now][i] == -1) next[now][i] = next[fail[now]][i]; else { fail[next[now][i]] = next[fail[now]][i]; q.push(next[now][i]); } } } }; Trie ac; char str[30]; char str1[30]; char str2[30]; int dp[110][2000][1<<6]; int main() { int T; cin >> T; int n, l; while (T--) { scanf("%d%d", &n, &l); ac.init(); for (int i = 0; i < n; i++) { scanf("%s", str); ac.insert(str,i, 1); int len1 = strlen(str); for (int j = 1; j < len1; j++) { memset(str1, '\0', sizeof(str1)); memset(str2, '\0', sizeof(str2));//str1是後半部分,str2是前半部分。 reverse_copy(str+j, str+len1, str1);//將str串的j到len1的部分反轉存入str1中。 reverse(str1, str1+strlen(str1));//將str1串反轉。 reverse_copy(str+0, str+j, str2); int len2 = min(strlen(str1), strlen(str2)); int flag = 1; for (int k = 0; k < len2; k++) { if (str1[k] == str2[k]) { flag = 0; break; } } if (!flag) continue; if (strlen(str1) > strlen(str2)) { reverse(str1, str1+strlen(str1)); for (int k = 0; k < strlen(str1); k++) if (str1[k] == '0') str1[k] = '1'; else str1[k] = '0'; ac.insert(str1, i, 0); } else { reverse(str2, str2+strlen(str2)); ac.insert(str2, i, 0); } } reverse(str, str+strlen(str)); for (int j = 0; j < strlen(str); j++) if (str[j] == '0') str[j] = '1'; else str[j] = '0'; ac.insert(str, i, 1); } ac.build(); memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; int m = l; for (int i = 0; i < m; i++) for (int j = 0; j < ac.L; j++) { for (int w = 0; w < (1<<n); w++) { if(dp[i][j][w] == 0) continue; for (int t = 0; t < 2; t++) { int next1 = ac.next[j][t]; if (i != m-1) dp[i+1][next1][w|ac.end1[next1]] += dp[i][j][w], dp[i+1][next1][w|ac.end1[next1]] %= MOD; else dp[i+1][next1][w|ac.end1[next1]|ac.end2[next1]] += dp[i][j][w],dp[i+1][next1][w|ac.end1[next1]|ac.end2[next1]] %= MOD;//當到L位時特殊處理。 } } } long long int sum = 0; for (int i = 0; i < ac.L; i++) { sum += dp[m][i][(1<<n)-1]; sum %= MOD; } cout << sum << endl; } return 0; }