首页 > 其他分享 >P4569 [BJWC2011] 禁忌 题解

P4569 [BJWC2011] 禁忌 题解

时间:2024-02-24 14:44:37浏览次数:29  
标签:frac 题解 P4569 tot times vis BJWC2011 alphabet Son

题目传送门

前置知识

AC 自动机 | 矩阵加速递推

解法

  • 对于一段固定的文本串,由于重叠的模式串不对伤害产生贡献,故考虑贪心,每碰到出现一个模式串将其分为一段,最终这个文本串的伤害就是划分次数。
  • 类似 luogu P3193 [HNOI2008] GT考试 ,令 \(f_{i,j}\) 表示前 \(i\) 个字符,当前运行到 \(AC\) 自动机的 \(j\) 状态时受到的概率。状态转移方程为 \(\begin{cases}f_{i+1,k}=\sum\limits_{j=0}^{tot} [k \in Son(j)] \times [vis_{k}=0] \times f_{i,j} \times \frac{1}{alphabet} & k \ne 0 \\ f_{i+1,0}=\sum\limits_{j=0}^{tot}\sum\limits_{k=0}^{tot}[k \in Son(j)] \times [vis_{k}=1] \times f_{i,j} \times \frac{1}{alphabet} \end{cases}\),其中 \(vis_{k}\) 表示 \(AC\) 自动机上是否存在以 \(k\) 结尾的字符串。另外,若 \(vis_{k}=1\),则可能会受到一次伤害,有 \(ans=ans+f_{i,j} \times \frac{1}{alphabet}\)。
  • 因 \(len \le 10^{9}\),故需要矩阵加速递推。
  • 令 \(F_{t}=\begin{bmatrix} f_{t,0} & f_{t,1} & f_{t,2} & \dots & f_{t,tot} & \sum\limits_{i=1}^{t-1}f_{i,0} \end{bmatrix}\),容易有 \(\begin{aligned} F_{t} &=\begin{bmatrix} f_{t,0} & f_{t,1} & f_{t,2} & \dots & f_{t,tot} & ans_{t} \end{bmatrix} \\ &=\begin{bmatrix} f_{t-1,0} & f_{t-1,1} & f_{t-1,2} & \dots & f_{t-1,tot} & ans_{t-1} \end{bmatrix} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix} \\ &=\begin{bmatrix} f_{t-2,0} & f_{t-2,1} & f_{t-2,2} & \dots & f_{t-2,tot} & ans_{t-2} \end{bmatrix} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix}^{2} \\ &=\begin{bmatrix} f_{t-3,0} & f_{t-3,1} & f_{t-3,2} & \dots & f_{t-3,tot} & ans_{t-1} \end{bmatrix} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix}^{3} \\ &=\dots \\ &=\begin{bmatrix} f_{0,0} & f_{0,1} & f_{0,2} & \dots & f_{0,tot} & ans_{0} \end{bmatrix} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix}^{t} \\ &=\begin{bmatrix} 1 & 0 & 0 & \dots & 0 & 0 \end{bmatrix} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix}^{t} \\ &=F_{0} \times \begin{bmatrix} \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(0)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(0)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(0)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(0)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(1)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(1)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(1)] \times [vis_{i}=1]}{alphabet} \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(2)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(2)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(2)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(2)] \times [vis_{i}=1]}{alphabet} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} & \frac{[1 \in Son(tot)] \times [vis_{1}=0]}{alphabet} & \frac{[2 \in Son(tot)] \times [vis_{2}=0]}{alphabet} & \dots & \frac{[tot \in Son(tot)] \times [vis_{tot}=0]}{alphabet} & \sum\limits_{i=0}^{tot}\frac{[i \in Son(tot)] \times [vis_{i}=1]}{alphabet} \\ 0 & 0 & 0 & \dots & 0 & 1 \end{bmatrix}^{t} \end{aligned}\)。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define ull unsigned long long
#define sort stable_sort 
#define endl '\n'
struct Matrix
{
	long double ma[80][80];
	Matrix()
	{
		memset(ma,0,sizeof(ma));
	}
}f,a;
int trie[80][30],fail[80],vis[80],tot=0;
char s[80];
Matrix mul(Matrix a,Matrix b,int n,int m,int k)
{
	Matrix c;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			for(int h=1;h<=m;h++)
			{
				c.ma[i][j]=c.ma[i][j]+a.ma[i][h]*b.ma[h][j];
			}
		}
	}
	return c;
}
Matrix qpow(Matrix a,int b,int n)
{
	Matrix ans;
	for(int i=1;i<=n;i++)
	{
		ans.ma[i][i]=1.0;
	}
	while(b>0)
	{
		if(b&1)
		{
			ans=mul(ans,a,n,n,n);
		}
		b>>=1;
		a=mul(a,a,n,n,n);
	}
	return ans;
}
int val(char x)
{
	return x-'a'+1;
}
void insert(char s[],int len)
{
	int x=0,i;
	for(i=1;i<=len;i++)
	{
		if(trie[x][val(s[i])]==0)
		{
			tot++;
			trie[x][val(s[i])]=tot;
		}
		x=trie[x][val(s[i])];
	}
	vis[x]=1;
}
void build(long double alphabet)
{
	int x,i;
	queue<int>q;
	for(i=1;i<=alphabet;i++)
	{
		if(trie[0][i]!=0)
		{
			fail[trie[0][i]]=0;
			q.push(trie[0][i]);
		}
	}
	while(q.empty()==0)
	{
		x=q.front();
		q.pop();
		for(i=1;i<=alphabet;i++)
		{
			if(trie[x][i]==0)
			{
				trie[x][i]=trie[fail[x]][i];
			}
			else
			{
				fail[trie[x][i]]=trie[fail[x]][i];
				vis[trie[x][i]]|=vis[fail[trie[x][i]]];//继承标记
				q.push(trie[x][i]);
			}
		}
	}
}
int main()
{
	int b,nn,n,m,k,i,j;
	long double alphabet;
	cin>>nn>>b>>alphabet;
	for(i=1;i<=nn;i++)
	{
		cin>>(s+1);
		insert(s,strlen(s+1));
	}
	build(alphabet);
	n=1;
	m=k=tot+2;
	f.ma[1][1]=a.ma[m][m]=1;
	for(i=0;i<=tot;i++)
	{
		for(j=1;j<=alphabet;j++)
		{
			if(vis[trie[i][j]]==0)
			{
				a.ma[i+1][trie[i][j]+1]+=1.0/alphabet;
			}
			else
			{
				a.ma[i+1][1]+=1.0/alphabet;
				a.ma[i+1][m]+=1.0/alphabet;
			}
		}
	}
	printf("%.12Lf\n",mul(f,qpow(a,b,m),n,m,k).ma[1][m]);
	return 0;
}

标签:frac,题解,P4569,tot,times,vis,BJWC2011,alphabet,Son
From: https://www.cnblogs.com/The-Shadow-Dragon/p/18031060

相关文章

  • AT_abc341_g [ABC341G] Highest Ratio 题解
    题目传送门前置知识单调栈简化题意给定一个长度为\(n\)的序列\(a\)。对于所有的\(l(1\lel\len)\),均求出\(\max\limits_{r=l}^{n}\{\frac{\sum\limits_{i=l}^{r}a_{i}}{r-l+1}\}\)。解法令\(sum_{i}=\sum\limits_{j=1}^{i}a_{j},P_{i}=(i,sum_{i})\),则有\(\beg......
  • P10139 [USACO24JAN] Nap Sort G 题解
    DescriptionBessie正在尝试使用她自己的排序算法对一个整数数组进行排序。她有一堆共\(N\)(\(1\leN\le2\cdot10^5\))个整数\(a_1,a_2,\ldots,a_N\)(\(1\lea_i\le10^{11}\)),她将会按排序顺序将这些数放入一个单独的数组中。她反复查找这堆数中的最小数,将其删除,同时将其添加到......
  • UOJ228/HDU5828 基础数据结构练习题/Rikka with Sequence 题解(势能线段树)
    势能线段树。如果线段树上一个节点的\(\max-\min\ge2\),我们称其为关键节点,考虑定义势能\(\phi\)为线段树上关键节点的个数。对于每次开方操作,如果当前节点为关键节点,则暴力递归左右儿子修改,否则:如果当前节点\(\max=\min\)或\(\max=\min+1\)且\(\max\)不是完全平方数,......
  • UVA12421 (Jiandan) Mua (I) - Lexical Analyzer 题解
    蒟蒻的第一篇紫题题解!题目传送门思路一眼模拟,还是大模拟。不由得想起了我编了\(4\)个小时的猪国杀……输入首先处理输入,这里我们用一个字符串数组来存储所有的输入,然后再进行处理。while(getline(cin,sr))str[++cnt]=sr+'\n';处理时需要双重循环,注意如果遍历到空格要跳......
  • CF916E Jamie and Tree 题解
    题目链接:CF或者洛谷本题难点在于换根LCA与换根以后的子树范围寻找,重点讲解先说操作一,假如原根为\(1\)变为了\(x\),又变为了\(y\),那么其实\(y\)和\(x\)都可以看做由\(1\)变化而来的,即\(1\rightarrowx\)与\(1\rightarrowy\),原因很简单,我们可以把\(1\rightar......
  • P9901 『PG2』弯曲半平面直线同向图最大流 题解
    思路正解想不出,只好用网络流了网络流简介请戳这儿。这道题数据有点大用EK求最大流似乎过不了,所以本蒟蒻采用Dinic算法。Dinic算法Dinic算法相当于EK的优化。在原基础找增广路的基础上添加了一个分层操作,再通过深搜找阻塞流。分层从原点开始我们把可以通过一步到达的......
  • 记录pyinstaller 打包 pdfplumber 问题解决过程
    今天有一个pdf文件处理需求,使用pdfplumber库完成,python环境是3.11+win10pyinstaller5.10.1打包完成后,工具可以顺利打开,但是执行处理的时候报错File"pypdfium2_raw\bindings.py",line93,in<module>File"pypdfium2_raw\bindings.py",line83,in_register_library......
  • blazor 问题解决
    Cannotprovideavalueforproperty'ScrollToLocationHash'ontype'Microsoft.AspNetCore.Components.Routing.Router'.Thereisnoregisteredserviceoftype'Microsoft.AspNetCore.Components.Routing.IScrollToLocationHash'.异常信息......
  • Jenkins构建提示docker命令权限问题解决方法
    参考:https://zhuanlan.zhihu.com/p/568513293使用Jenkins构建时使用的用户为jenkins在使用docker命令时会报以下错误ERROR:permissiondeniedwhiletryingtoconnecttotheDockerdaemonsocketatunix:///var/run/docker.sock:Get"http://%2Fvar%2Frun%2Fdocker.soc......
  • P6646 [CCO2020] Shopping Plans 题解
    好好玩的题。思路对于前\(K\)小方案问题。我们可以考虑当前方案对下一个方案的转移。重点在于转移的最优化与不重不漏。只有一种种类假设没有\(l,r\)的限制怎么做。我们不妨把所有价格排序。发现一种状态转移到另一种状态,无异与将其中已选择的一个物品不选,选择他后面......