【题意】现在有个长度已知的字符串,你知道一些它的子串,问你这个字符串的可能的种数(这些子串可以重叠),如果种数 <= 42,那么就把按照字典序输出来。

【解题方法】先构造AC自动机,设 d[ u ][ len ][ st ]表示最后一个是 u ,已选长度为 len ,状态为 st 的剩余种数,则有方程:dp[ u ][ len ][ st ] = SIGMA(dp[ ch[u][i] ][ len+1 ][ st|val[ch[u][i]] ], 0 <= i < SIGMA_SIZE)。这里有一点要注意,由于一个节点有可能是很多字符串的终点,AC自动机 insert之后,在 get_fail 那里也要更新 val。输出结果那里,也是递归打印,不过有很重要的一个地方,那就是只有 d(u,len,st)> 0,才递归下去!还有这里AC自动机要是改造版的那种,正好做 DP。

【代码君】

//
//Created by just_sort 2016/10/12
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 200;
const int maxm = 26;
LL dp[maxn][40][1<<11];
bool vis[maxn][40][1<<11];
char tmp[40];
char s[40];
int n,m;
struct Acautomata{
    int ch[maxn][maxm],val[maxn],fail[maxn],root,sz;
    int newnode(){
        val[sz] = 0;
        memset(ch[sz],0,sizeof(ch[sz]));
        return sz++;
    }
    void init(){
        sz = 0;
        root = newnode();
    }
    void insert(char *s, int v){
        int u = root, len = strlen(s);
        for(int i = 0; i < len; i++){
            int now = s[i] - 'a';
            if(!ch[u][now]){
                ch[u][now] = newnode();
            }
            u = ch[u][now];
        }
        val[u] |= v;
    }
    void build(){
        queue <int> q;
        fail[0] = 0;
        for(int i = 0; i < maxm; i++){
            int u = ch[0][i];
            if(u){
                q.push(u); fail[u] = 0;
            }
        }
        while(q.size()){
            int u = q.front(); q.pop();
            for(int i = 0; i < maxm; i++){
                int v = ch[u][i];
                if(!v){
                    ch[u][i] = ch[fail[u]][i];
                    continue;
                }
                q.push(v);
                int j = fail[u];
                while(j && !ch[j][i]) j = fail[j];
                fail[v] = ch[j][i];
                val[v] |= val[fail[v]];
            }
        }
    }
    LL dfs(int u,int len,int st)
    {
        if(vis[u][len][st]) return dp[u][len][st];
        vis[u][len][st] = 1;
        LL &ans = dp[u][len][st];
        if(len == n){
            if(st == (1<<m)-1) return ans = 1;
            else{
                return ans = 0;
            }
        }
        ans = 0;
        for(int i = 0; i < maxm; i++){
            ans += dfs(ch[u][i], len+1, st|val[ch[u][i]]);
        }
        return ans;
    }
    void print_path(int u,int len,int st)
    {
        if(len == n)
        {
            for(int i = 1; i <= len; i++) printf("%c",tmp[i]);
            printf("\n");
            return ;
        }
        for(int i = 0; i < maxm; i++)
        {
            tmp[len+1] = 'a' + i;
            if(dp[ch[u][i]][len+1][st|val[ch[u][i]]])
            {
                print_path(ch[u][i],len+1,st|val[ch[u][i]]);
            }
        }
    }
}ac;
int main()
{
    int ks = 1;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        if(n == 0 && m == 0) break;
        ac.init();
        for(int i = 0; i < m; i++){
            scanf("%s",s);
            ac.insert(s, 1<<i);
        }
        ac.build();
        memset(vis,0,sizeof(vis));
        //memset(dp,0,sizeof(dp));
        LL ans = ac.dfs(0,0,0);
        //printf("%lld\n",ans);
        printf("Case %d: %lld suspects\n",ks++,ans);
        if(ans <= 42)
            ac.print_path(0,0,0);
    }
    return 0;
}