Description The alphabet of Freeland consists of exactly N letters.
Each sentence of Freeland language (also known as Freish) consists of
exactly M letters without word breaks. So, there exist exactly N^M
different Freish sentences.But after recent election of Mr. Grass Jr. as Freeland president some
words offending him were declared unprintable and all sentences
containing at least one of them were forbidden. The sentence S
contains a word W if W is a substring of S i.e. exists such k >= 1
that S[k] = W[1], S[k+1] = W[2], …,S[k+len(W)-1] = W[len(W)], where
k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a
forbidden sentence is to be put to jail for 10 years.Find out how many different sentences can be used now by freelanders
without risk to be put to jail for using it.Input The first line of the input file contains three integer numbers:
N – the number of letters in Freish alphabet, M – the length of all
Freish sentences and P – the number of forbidden words (1 <= N <= 50,
1 <= M <= 50, 0 <= P <= 10).The second line contains exactly N different characters – the letters
of the Freish alphabet (all with ASCII code greater than 32).The following P lines contain forbidden words, each not longer than
min(M, 10) characters, all containing only letters of Freish alphabet.Output Output the only integer number – the number of different
sentences freelanders can safely use.
与bzoj1030文本生成器【传送门】几乎一样,需要写高精。
详细题解见如上链接。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct bigint
{
int l,a[50];
bigint operator + (const bigint &b) const
{
int i,j,k,x,y;
bigint ans;
memset(ans.a,0,sizeof(ans.a));
for (i=1;i<=l||i<=b.l;i++)
{
ans.a[i]+=a[i]+b.a[i];
ans.a[i+1]=ans.a[i]/10000;
ans.a[i]%=10000;
}
if (l>b.l) ans.l=l;
else ans.l=b.l;
if (ans.a[ans.l+1]) ans.l++;
return ans;
}
}dp[60][3000],one,ans;
void prt(bigint p)
{
int i,j,k,m,n,q,x,y,z;
printf("%d",p.a[p.l]);
for (i=p.l-1;i>=1;i--)
{
x=p.a[i];
if (x<1000) printf("0");
if (x<100) printf("0");
if (x<10) printf("0");
printf("%d",x);
}
printf("\n");
}
int ord[500],t[3000][60],f[3000],si;
bool b[3000];
int main()
{
int i,j,k,l,m,n,p,q,x,y,z;
unsigned char c,s[20];
one.a[1]=one.l=1;
scanf("%d%d%d\n",&n,&m,&k);
for (i=1;i<=n;i++)
{
scanf("%c",&c);
ord[c]=i;
}
for (i=1;i<=k;i++)
{
scanf("%s",s+1);
p=0;
for (j=1;s[j]!='\0';j++)
{
if (!t[p][ord[s[j]]]) t[p][ord[s[j]]]=++si;
p=t[p][ord[s[j]]];
}
b[p]=1;
}
queue<int> que;
for (i=1;i<=n;i++)
if (t[0][i]) que.push(t[0][i]);
while (!que.empty())
{
x=que.front();
que.pop();
for (i=1;i<=n;i++)
if (t[x][i])
{
f[t[x][i]]=t[f[x]][i];
que.push(t[x][i]);
if (b[f[t[x][i]]]) b[t[x][i]]=1;
}
else t[x][i]=t[f[x]][i];
}
for (i=1;i<=n;i++)
if (!b[t[0][i]]) dp[1][t[0][i]]=dp[1][t[0][i]]+one;
for (i=1;i<m;i++)
for (j=0;j<=si;j++)
for (k=1;k<=n;k++)
if (!b[t[j][k]])
dp[i+1][t[j][k]]=dp[i+1][t[j][k]]+dp[i][j];
for (i=0;i<=si;i++)
ans=ans+dp[m][i];
prt(ans);
}