[BJWC2011]禁忌 AC 自动机 概率与期望
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<cstdlib> #include<iostream> using namespace std; void setIO(string a){ freopen ((a+ ".in" ).c_str(), "r" ,stdin); } #define db long double #define maxn 100000 char arr[maxn]; int sigma, nodes, n; struct matrix{ long double mat[150][150]; }unit; void init(matrix &a){ for ( int i=0;i<=n;++i) for ( int j=0;j<=n;++j) a.mat[i][j]=0; } void get(matrix &a){ init(a); for ( int i=0;i<=n;++i) a.mat[i][i]=1; } matrix operator*(matrix a,matrix b){ matrix c; init(c); for ( int i=0;i<=n;++i) for ( int j=0;j<=n;++j) for ( int k=0;k<=n;++k) c.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; return c; } matrix operator^(matrix a, int p){ matrix ans; get(ans); while (p){ if (p&1) ans=ans*a; a=a*a; p>>=1; } return ans; } struct Automaton{ int ch[maxn][30], fail[maxn], tag[maxn]; #define idx str[i]-'a' void insert( char str[]){ int cnt= strlen (str),j=0; for ( int i=0;i<cnt;++i) { if (!ch[j][idx]) ch[j][idx]=++nodes; j=ch[j][idx]; } tag[j]= true , n=nodes+1; } queue< int >Q; bool vis[maxn]; void build(){ for ( int i=0;i<sigma;++i) if (ch[0][i]) Q.push(ch[0][i]); while (!Q.empty()){ int u=Q.front();Q.pop(); for ( int i=0;i<sigma;++i){ int r=ch[u][i]; if (!r){ ch[u][i]=ch[fail[u]][i]; continue ;} Q.push(r), fail[r]=ch[fail[u]][i], tag[r]|=tag[fail[r]]; } } init(unit); Q.push(0), vis[0]= true ; long double tmp=( long double )1/sigma; while (!Q.empty()){ int u=Q.front();Q.pop(); for ( int i=0;i<sigma;++i){ if (!vis[ch[u][i]]) vis[ch[u][i]]= true , Q.push(ch[u][i]); if (tag[ch[u][i]]) unit.mat[u][0]+=tmp, unit.mat[u][n]+=tmp; else unit.mat[u][ch[u][i]]+=tmp; } unit.mat[n][n]=1; } } }aho; int main(){ //setIO("input"); int m,len; scanf ( "%d%d%d" ,&m,&len,&sigma); for ( int i=1;i<=m;++i) scanf ( "%s" ,arr), aho.insert(arr); aho.build(); matrix g=unit^len; printf ( "%.10f" ,( double )g.mat[0][n]); return 0; } |
说实话还是有点不太理解.....
Code:
分类:
数学 - 概率与期望
, 字符串 - AC自动机
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!