UVaLive 4126 - Password Suspects (AC自动机 DP)
2014-10-01 20:36
363 查看
UVALive - 4126 Password Suspects
Back] [Status] Description ![]() You are the computer whiz for the secret organization known as the Sneaky Underground Smug Perpetrators of Evil Crimes and Thefts. The target for SUSPECT's latest evil crime is their greatest foe, the Indescribably Clever Policemen's Club, and everything is prepared. Everything, except for one small thing: the secret password for ICPC's main computer system. The password is known to consist only of lowercase letters `a'-`z'. Furthermore, through various sneaky observations, you have been able to determine the length of the password, as well as a few (possibly overlapping) substrings of the password, though you do not know exactly where in the password they occur. For instance, say that you know that the password is 10 characters long, and that you have observed the substrings ``hello" and ``world". Then the password must be either ``helloworld" or ``worldhello". The question is whether this information is enough to reduce the number of possible passwords to a reasonable amount. To answer this, your task is to write a program that determines the number of possible passwords and, if there are at most 42 of them, prints them all. Input The input file contains several test cases. Each test case begins with a line containing two integers N and M(1 ![]() N ![]() 25, 0 ![]() M ![]() 10) , giving the length of the password and the number of known substrings respectively. This is followed by M lines, each containing a known substring. Each known substring consists of between 1 and 10 lowercase letters `a'-`z'. The last test case is followed by a line containing two zeroes. Output For each test case, print the case number (beginning with 1) followed by `Ysuspects', where Y is the number of possible passwords for this case. If the number of passwords is at most 42, then output all possible passwords in alphabetical order, one per line. The input will be such that the number of possible passwords at most 1015 . Sample Input 10 2 hello world 10 0 4 1 icpc 0 0 Sample Output Case 1: 2 suspects helloworld worldhello Case 2: 141167095653376 suspects Case 3: 1 suspects icpc Source [Submit] [Go Back] [Status] |
已知一个长为n的字符串包含m个字串,问有多少种串满足
把m个字串构造为AC自动机,然后在这AC自动机上面进行DP
dp[len][u][S] 表示字串串长度为len,当前处于u位置,拥有S集合中的字串的方法数
当前的策略为选择走第len位填一个字符c,然后顺着自动机走。
自动机的每个节点保存拥有的字符串集合
#include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <map> #include <cmath> #include <queue> #include <set> using namespace std; //#define WIN #ifdef WIN typedef __int64 LL; #define iform "%I64d" #define oform "%I64d\n" #define oform1 "%I64d" #else typedef long long LL; #define iform "%lld" #define oform "%lld\n" #define oform1 "%lld" #endif #define S64I(a) scanf(iform, &(a)) #define P64I(a) printf(oform, (a)) #define P64I1(a) printf(oform1, (a)) #define REP(i, n) for(int (i)=0; (i)<n; (i)++) #define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++) const int INF = 0x3f3f3f3f; const double eps = 1e-9; const double PI = (4.0*atan(1.0)); const int MAX_NODE = 250 + 20; const int SIGMA_SIZE = 26; int ch[MAX_NODE][SIGMA_SIZE]; int Fail[MAX_NODE], last[MAX_NODE]; int val[MAX_NODE]; int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } void insert(char * s, int v) { int u = 0, n = strlen(s); for(int i=0; i<n; i++) { int c = s[i] - 'a'; if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] |= v; } void getFail() { queue<int> Q; Fail[0] = 0; for(int c=0; c<SIGMA_SIZE; c++) { int u = ch[0][c]; if(u) { Fail[u] = 0; Q.push(u); last[u] = 0; } } while(!Q.empty()) { int r = Q.front(); Q.pop(); for(int i=0; i<SIGMA_SIZE; i++) { int u = ch[r][i]; if(!u) { ch[r][i] = ch[Fail[r]][i]; continue; } Q.push(u); int v = Fail[r]; while(v && !ch[v][i]) v = Fail[v]; Fail[u] = ch[v][i]; last[u] = val[Fail[u]] ? Fail[u] : last[Fail[u]]; val[u] |= val[last[u]]; } } } const int MAXS = (1<<10) + 10; LL dp[26][MAX_NODE][MAXS]; int n, S_ALL; vector<string> anss; char ss[40]; LL dfs(int len, int cur, int S) { if(len == n) { return S == S_ALL; } LL & ans = dp[len][cur][S]; if(ans != -1) return ans; ans = 0; for(int i=0; i<SIGMA_SIZE; i++) { int v = ch[cur][i]; ss[len] = i + 'a'; ans += dfs(len+1, v, S|val[v]); } return ans; } void getAns(int len, int cur, int S) { if(len == n) { if(S == S_ALL) { ss[len] = '\0'; anss.push_back(string(ss)); } return ; } for(int i=0; i<SIGMA_SIZE; i++) { int v = ch[cur][i]; ss[len] = i + 'a'; if(dp[len+1][v][S|val[v]]) getAns(len+1, v, S|val[v]); } } char str[40]; int main() { int m; int kase = 1; while(scanf("%d%d", &n, &m) != EOF && n+m) { init(); for(int i=0; i<m; i++) { scanf("%s", str); insert(str, 1<<i); } getFail(); memset(dp, -1, sizeof(dp)); S_ALL = (1<<m) - 1; LL ans = dfs(0, 0, 0); printf("Case %d: %lld suspects\n", kase++, ans); if(ans <= 42) { anss.clear(); getAns(0, 0, 0); sort(anss.begin(), anss.end()); for(int i=0; i<anss.size(); i++) { puts(anss[i].c_str()); } } } return 0; }
相关文章推荐
- UVALive 4126 Password Suspects(AC自动机 套 DP)
- UVALive 4126 Password Suspects(AC自动机 + DP)
- UVaLive/LA 6806 Hari Merdeka(AC自动机,DP)
- UVALive 4126 (LA 4126) Password Suspects AC自动机 + DP + 剪枝dfs
- UVALive 3026 Period (KMP上的dp,学习ac自动机的前奏)
- UVaLive 3490 - Generator (AC自动机 期望DP 高斯消元)
- UVALive 4126 Password Suspects (AC自动机+DP)
- DP->UVALive 4764
- uvalive 6669 hidden tree(好壮压dp)
- UVALive - 7484 Association for the Country of Mububa(dp)
- UVALive 5002/ lightoj 1382 The Queue(树形DP)
- UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)
- UVALive - 6872 Restaurant Ratings 数位dp
- UVALive 7374 Racing Gems - dp,最长上升子序列
- [UVALive3675]Sorted bit sequence && 数位DP
- UVALive 4256 Salesmen (树DP,4级)
- UVALive 6462 状压DP
- UVALive - 4015 Caves 树形DP
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVALive 4670 AC自动机