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 andM (1N
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 `Y suspects', 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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct info
{
int P;
int next[26];
};
const int MaxN=25;
const int MaxM=10;
const int MaxL=10;
const int MaxOut=42;
char word[MaxM][MaxL+1],list[MaxOut+1][MaxN+1];
long long ans,list_num;
int cases,n,m,c,sum;
info prefix[MaxM*MaxL+1];
long long f[MaxN+1][1<<MaxM][MaxM*MaxL+1];
int tmp[MaxOut+1];
void refresh(int x)
{
memset(prefix[x].next,0,sizeof(prefix[x].next));
prefix[x].P=0;
}
int check(int s)
{
int i,now;
now=1;
for(i=s;i<=tmp[0];i++)
if(prefix[now].next[tmp[i]]>0)
now=prefix[now].next[tmp[i]];
else
return 0;
return now;
}
void work(int x)
{
int i,j,k;
tmp[0]++;
for(i=0;i<26;i++)
if(prefix[x].next[i]>0)
{
tmp[tmp[0]]=i;
work(prefix[x].next[i]);
}
else
{
tmp[tmp[0]]=i;
for(j=1;j<=tmp[0]+1;j++)
{
k=check(j);
if(k!=0)
{
prefix[x].next[i]=k;
break;
}
}
}
tmp[0]--;
}
bool cmp(int x,int y)
{
return (strcmp(list[x],list[y])<0);
}
void rework(int Len,int l,int now)
{
int i,L,k;
if(f[Len][l][now]==0)
return;
if(Len==0)
{
list_num++;
for(i=0;i<n;i++)
list[list_num][i]=tmp[i+1]+'a';
list[list_num][n]='\0';
return;
}
for(L=0;L<26;L++)
for(i=1;i<=sum;i++)
if(prefix[i].next[L]==now)
{
tmp[Len]=L;
rework(Len-1,l,i);
if(prefix[now].P)
rework(Len-1,l-(1<<(prefix[now].P-1)),i);
}
}
bool init()
{
int i,j,k,now,l,t;
scanf("%d %d",&n,&m);
if(n==0)
return true;
for(i=1;i<=m;i++)
scanf("%s",word[i]);
for(i=m;i>=1;i--)
for(j=1;j<=m;j++)
if(strlen(word[j])>=strlen(word[i])&&i!=j)
{
for(k=0;k<=strlen(word[j]-strlen(word[i]));k++)
{
t=0;
for(l=0;word[i][l]!='\0';l++)
if(word[i][l]!=word[j][l+k])
t=1;
if(t==0)
break;
}
if(t==0)
{
for(k=i+1;k<=m;k++)
{
for(l=0;'a'<=word[k][l]&&word[k][l]<='z';l++)
word[k-1][l]=word[k][l];
word[k-1][l]='\0';
}
m--;
break;
}
}
sum=1;refresh(1);
for(i=1;i<=m;i++)
{
now=1;
for(j=0;'a'<=word[i][j]&&word[i][j]<='z';j++)
{
if(prefix[now].next[word[i][j]-'a']==0)
{
sum++;
refresh(sum);
prefix[now].next[word[i][j]-'a']=sum;
}
now=prefix[now].next[word[i][j]-'a'];
}
prefix[now].P=i;
}
tmp[0]=0;
work(1);
memset(f,0,sizeof(f));
f[0][0][1]=1;
return false;
}
void DP()
{
int i,j,k,c;
for(i=0;i<n;i++)
for(j=0;j<(1<<m);j++)
for(k=1;k<=sum;k++)
if(f[i][j][k])
for(c=0;c<26;c++)
f[i+1][j|(1<<(prefix[prefix[k].next[c]].P-1))][prefix[k].next[c]]+=f[i][j][k];
}
void write()
{
int i;
ans=0;
for(i=1;i<=sum;i++)
ans+=f[n][(1<<m)-1][i];
printf("Case %d: %lld suspects\n",++cases,ans);
if(ans<=MaxOut)
{
list_num=0;
for(i=1;i<=sum;i++)
if(f[n][(1<<m)-1][i]>0)
rework(n,(1<<m)-1,i);
for(i=1;i<=ans;i++)
tmp[i]=i;
sort(tmp+1,tmp+ans+1,cmp);
for(i=1;i<=ans;i++)
printf("%s\n",list[tmp[i]]);
}
}
int main()
{
while(1)
{
if(init())
break;
DP();
write();
}
return 0;
}