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[];
bigint operator + (const bigint &b) const
{
int i,j,k,x,y;
bigint ans;
memset(ans.a,,sizeof(ans.a));
for (i=;i<=l||i<=b.l;i++)
{
ans.a[i]+=a[i]+b.a[i];
ans.a[i+]=ans.a[i]/;
ans.a[i]%=;
}
if (l>b.l) ans.l=l;
else ans.l=b.l;
if (ans.a[ans.l+]) ans.l++;
return ans;
}
}dp[][],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-;i>=;i--)
{
x=p.a[i];
if (x<) printf("0");
if (x<) printf("0");
if (x<) printf("0");
printf("%d",x);
}
printf("\n");
}
int ord[],t[][],f[],si;
bool b[];
int main()
{
int i,j,k,l,m,n,p,q,x,y,z;
unsigned char c,s[];
one.a[]=one.l=;
scanf("%d%d%d\n",&n,&m,&k);
for (i=;i<=n;i++)
{
scanf("%c",&c);
ord[c]=i;
}
for (i=;i<=k;i++)
{
scanf("%s",s+);
p=;
for (j=;s[j]!='\0';j++)
{
if (!t[p][ord[s[j]]]) t[p][ord[s[j]]]=++si;
p=t[p][ord[s[j]]];
}
b[p]=;
}
queue<int> que;
for (i=;i<=n;i++)
if (t[][i]) que.push(t[][i]);
while (!que.empty())
{
x=que.front();
que.pop();
for (i=;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]]=;
}
else t[x][i]=t[f[x]][i];
}
for (i=;i<=n;i++)
if (!b[t[][i]]) dp[][t[][i]]=dp[][t[][i]]+one;
for (i=;i<m;i++)
for (j=;j<=si;j++)
for (k=;k<=n;k++)
if (!b[t[j][k]])
dp[i+][t[j][k]]=dp[i+][t[j][k]]+dp[i][j];
for (i=;i<=si;i++)
ans=ans+dp[m][i];
prt(ans);
}