hdoj 2825 Wireless Password 【AC自动机 + 状压dp】
2015-10-13 22:38
471 查看
Wireless PasswordTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5090 Accepted Submission(s): 1613 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters 'a'-'z', and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping). For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'. Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords. Input There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters 'a'-'z'. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed. Output For each test case, please output the number of possible passwords MOD 20090717. Sample Input 10 2 2 hello world 4 1 1 icpc 10 0 0 0 0 0 Sample Output 2 1 14195065 |
思路:构建AC自动机,根据Trie状态转移图统计Trie上每个节点对应的状态,并用State[]记录。
把m个模式串压缩成1<<m个状态,用dp[i][S][j];//表示长度为i的串在Trie的S节点包含状态j的总方案数
得到状态转移方程
dp[ i+1 ][ next[S][l] ][ j|State[next[S][l]] ] += dp[i][S][j],其中l为S节点的所有子节点。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 105 #define INF 0x3f3f3f3f #define MOD 20090717 #define LL long long using namespace std; int have[1<<10];//当前状态 包含多少个模式串 int n, m, k; void gethave() { for(int i = 0; i < (1<<10); i++) { have[i] = 0; for(int j = 0; j < 10; j++) { if(i & (1<<j)) have[i]++; } } } struct Trie { int next[MAXN][30], fail[MAXN], State[MAXN]; int L, root; int newnode() { for(int i = 0; i < 26; i++) next[L][i] = -1; State[L++] = 0; return L-1; } void init() { L = 0; root = newnode(); } void Insert(char *s, int id) { int len = strlen(s); int now = root; for(int i = 0; i < len; i++) { if(next[now][s[i]-'a'] == -1) next[now][s[i]-'a'] = newnode(); now = next[now][s[i]-'a']; } State[now] |= (1<<id); } void Build() { queue<int> Q; fail[root] = root; for(int i = 0; i < 26; 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(); State[now] |= State[fail[now]];//每个节点所包含模式串状态 for(int i = 0; i < 26; 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]); } } } } //把m个模式串状态压缩成 0——1<<m LL dp[30][MAXN][1<<10];//表示长度为i的串在Trie的j节点包含状态k的总数 void solve() { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for(int i = 0; i < n; i++) { for(int S = 0; S < L; S++) { for(int j = 0; j < (1<<m); j++) { if(dp[i][S][j] > 0) { for(int l = 0; l < 26; l++) { int nextnode = next[S][l]; int nextState = j | State[nextnode]; dp[i+1][nextnode][nextState] += dp[i][S][j], dp[i+1][nextnode][nextState] %= MOD; } } } } } LL ans = 0; for(int i = 0; i < (1<<m); i++) { if(have[i] < k) continue; for(int j = 0; j < L; j++) ans = (ans + dp [j][i]) % MOD; } printf("%lld\n", ans); } }; Trie ac; char str[15]; int main() { gethave(); while(scanf("%d%d%d", &n, &m, &k), n||m||k) { ac.init(); for(int i = 0; i < m; i++) scanf("%s", str), ac.Insert(str, i); ac.Build(); ac.solve(); } return 0; }
相关文章推荐
- storm 开发系列一 第一个程序
- 使用swfupload进行异步文件上传(java版)
- 5 Smallest multiple - Project Eulder
- java23种设计模式之装饰模式(Decorator)
- 设计模式之单例模式
- free命令
- Dropping water balloons
- 优化SQL查询
- 那些看着高大上的名词
- html元素与间距
- bin/ld.exe: error: cannot find -landroid等问题
- 自定义View画报表
- poj 2002 Squares 哈希
- 在App中添加数据统计功能(友盟)
- Coursera_Stanford_ML_ex4_神经网络训练 作业记录
- EffectiveC#9--明白几个相等运算之间的关系
- 文章标题
- MySQL重置密码
- 手机usb线的区别
- iOS支付