题解 P4569 【[BJWC2011]禁忌】
考虑用
要最大化魔法分割后得到的禁忌串数目,最优情况肯定为在一个串中每个禁忌串的右端点进行分割。对应到
若用朴素的
先构造初始矩阵,
同时新增一个状态
构造矩阵时,分情况讨论。设
具体实现细节看代码吧。
#include<bits/stdc++.h>
#define maxn 110
using namespace std;
typedef long double ld;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
int n,l,alph,tot,root;
ld P;
int trie[maxn][30],fail[maxn];
bool end[maxn];
char s[maxn];
struct matrix
{
ld a[maxn][maxn];
}m,e;
matrix operator *(const matrix &x,const matrix &y)
{
matrix z;
memset(z.a,0,sizeof(z.a));
for(int k=root;k<=tot+1;++k)
for(int i=root;i<=tot+1;++i)
for(int j=root;j<=tot+1;++j)
z.a[i][j]+=x.a[i][k]*y.a[k][j];
return z;
}
matrix qp(matrix x,int y)
{
matrix t=e;
while(y)
{
if(y&1) t=t*x;
x=x*x;
y>>=1;
}
return t;
}
void insert()
{
int len=strlen(s+1),p=root;
for(int i=1;i<=len;++i)
{
int ch=s[i]-'a';
if(!trie[p][ch]) trie[p][ch]=++tot;
p=trie[p][ch];
}
end[p]=true;
}
void build()
{
queue<int> q;
for(int i=0;i<alph;++i)
if(trie[root][i])
q.push(trie[root][i]);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<alph;++i)
{
int y=trie[x][i];
if(y)
{
fail[y]=trie[fail[x]][i];
end[y]|=end[fail[y]],q.push(y);
}
else trie[x][i]=trie[fail[x]][i];
}
}
e.a[tot+1][tot+1]=m.a[tot+1][tot+1]=1;
for(int x=root;x<=tot;++x)
{
e.a[x][x]=1;
for(int ch=0;ch<alph;++ch)
{
int y=trie[x][ch];
if(end[y]) m.a[x][tot+1]+=P,m.a[x][root]+=P;
else m.a[x][y]+=P;
}
}
}
int main()
{
read(n),read(l),read(alph),P=(ld)1.0/(ld)alph;
for(int i=1;i<=n;++i)
scanf("%s",s+1),insert();
build(),m=qp(m,l);
printf("%Lf",m.a[root][tot+1]);
return 0;
}