题目链接

BZOJ2553

题解

话说在前,此题卡精度,最好开long double

先建\(AC\)自动机

求期望,逆着求,设\(f[i][j]\)为长度为\(i\)的串,当前匹配AC自动机\(j\)节点,之后能产生伤害的期望值

枚举转移,如果转移到一个单词节点,因为产生伤害的单词间不能相连,就直接跳回根节点

矩乘优化一下即可

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<queue>
  6. #include<cmath>
  7. #include<iomanip>
  8. #include<map>
  9. #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
  10. #define REP(i,n) for (int i = 1; i <= (n); i++)
  11. #define mp(a,b) make_pair<int,int>(a,b)
  12. #define cls(s) memset(s,0,sizeof(s))
  13. #define cp pair<int,int>
  14. #define LL long long int
  15. using namespace std;
  16. const int maxn = 80,maxm = 100005,INF = 1000000000;
  17. inline int read(){
  18. int out = 0,flag = 1; char c = getchar();
  19. while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
  20. while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
  21. return out * flag;
  22. }
  23. char s[6][20];
  24. int n,L,A,len[6];
  25. int ch[maxn][26],fail[maxn],val[maxn],cnt;
  26. void ins(int p){
  27. int u = 0,id;
  28. for (int i = 1; i <= len[p]; i++){
  29. id = s[p][i] - 'a';
  30. u = ch[u][id] ? ch[u][id] : (ch[u][id] = ++cnt);
  31. }
  32. val[u] = true;
  33. }
  34. void dfs(int u,int f){
  35. if (val[f]) val[u] = true;
  36. for (int i = 0; i < A; i++)
  37. if (ch[u][i]) dfs(ch[u][i],u);
  38. }
  39. void getf(){
  40. queue<int> q;
  41. for (int i = 0; i < A; i++) if (ch[0][i]) q.push(ch[0][i]);
  42. int u,v;
  43. while (!q.empty()){
  44. u = q.front(); q.pop();
  45. for (int i = 0; i < A; i++){
  46. v = ch[u][i];
  47. if (!v){
  48. ch[u][i] = ch[fail[u]][i];
  49. continue;
  50. }
  51. fail[v] = ch[fail[u]][i];
  52. q.push(v);
  53. }
  54. }
  55. }
  56. struct Matrix{
  57. long double s[maxn][maxn];
  58. int n,m;
  59. Matrix(){cls(s);n = m = 0;}
  60. }C,F;
  61. inline Matrix operator *(const Matrix& a,const Matrix b){
  62. Matrix c;
  63. if (a.m != b.n) return c;
  64. c.n = a.n; c.m = b.m;
  65. for (int i = 0; i < c.n; i++)
  66. for (int j = 0; j < c.m; j++)
  67. for (int k = 0; k < a.m; k++)
  68. c.s[i][j] += a.s[i][k] * b.s[k][j];
  69. return c;
  70. }
  71. inline Matrix qpow(Matrix a,int b){
  72. Matrix re; re.n = re.m = a.n;
  73. for (int i = 0; i < re.n; i++) re.s[i][i] = 1;
  74. for (; b; b >>= 1,a = a * a)
  75. if (b & 1) re = re * a;
  76. return re;
  77. }
  78. int main(){
  79. n = read(); L = read(); A = read();
  80. REP(i,n){
  81. scanf("%s",s[i] + 1),len[i] = strlen(s[i] + 1);
  82. ins(i);
  83. }
  84. dfs(0,0);
  85. getf();
  86. C.n = C.m = cnt + 2; long double x = 1.0 / A;
  87. for (int i = 0; i <= cnt; i++){
  88. if (val[i]) continue;
  89. for (int j = 0; j < A; j++){
  90. if (val[ch[i][j]]){
  91. C.s[i][cnt + 1] += x;
  92. C.s[i][0] += x;
  93. }
  94. else {
  95. C.s[i][ch[i][j]] += x;
  96. }
  97. }
  98. }
  99. C.s[cnt + 1][cnt + 1] = 1;
  100. F.n = cnt + 2; F.m = 1;
  101. F.s[cnt + 1][0] = 1;
  102. Matrix Fn = qpow(C,L) * F;
  103. long double ans = Fn.s[0][0];
  104. cout << fixed <<setprecision(12) << ans<<endl;
  105. return 0;
  106. }

BZOJ2553 [BeiJing2011]禁忌 【AC自动机 + dp + 矩乘优化】的更多相关文章

  1. BZOJ2553[BeiJing2011]禁忌——AC自动机+概率DP+矩阵乘法

    题目描述 Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi恢复了读心的能力…… 如今,在John已经成为传 ...

  2. BZOJ2553 [BeiJing2011]禁忌 AC自动机 矩阵

    原文链接http://www.cnblogs.com/zhouzhendong/p/8196279.html 题目传送门 - BZOJ2553 题意概括 引用一下lych大佬的: 在字母只有前alph ...

  3. 【BZOJ】2553: [BeiJing2011]禁忌 AC自动机+期望+矩阵快速幂

    [题意]给定n个禁忌字符串和字符集大小alphabet,保证所有字符在集合内.一个字符串的禁忌伤害定义为分割能匹配到最多的禁忌字符串数量(一个可以匹配多次),求由字符集构成的长度为Len的字符串的期望 ...

  4. 【BZOJ2553】[BeiJing2011]禁忌 AC自动机+期望DP+矩阵乘法

    [BZOJ2553][BeiJing2011]禁忌 Description Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平. ...

  5. bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...

  6. bzoj 2553 [BeiJing2011]禁忌——AC自动机+概率DP+矩阵

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2553 看了题解才会…… 首先,给定一个串,最好的划分方式是按禁忌串出现的右端点排序,遇到能填 ...

  7. 【BZOJ 2553】[BeiJing2011]禁忌 AC自动机+期望概率dp

    我一开始想的是倒着来,发现太屎,后来想到了一种神奇的方法——我们带着一个既有期望又有概率的矩阵,偶数(2*id)代表期望,奇数(2*id+1)代表概率,初始答案矩阵一列,1的位置为1(起点为0),工具 ...

  8. 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂

    [题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...

  9. POJ1625 Censored!(AC自动机+DP)

    题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...

随机推荐

  1. path.resolve()和path.join()的区别

    path.join()  组装路径.该方法的主要用途在于,会正确使用当前系统的路径分隔符,Unix系统是/,Windows系统是\.路径字符中可以使用..或../进行相对路径的计算,其它路径表示符会被 ...

  2. 银行系统ps:不太完善,蟹蟹评论

    # 主程序运行 import time from guanli import GuanLi from atm import ATM from user import User def main(): ...

  3. 聊一聊 Flex 中的 flex-grow、flex-shrink、flex-basis

    在使用 flex 布局的时候难以理解的是 flex-grow.flex-shrink.flex-basis 几个属性的用法,下面通过几个例子来演示. flex-basis flex-basis 用于设 ...

  4. Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking---随笔

    Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking DCF跟踪算法因边界效应,鲁棒性较差.SRD ...

  5. ThinkPHP - 5 - 学习笔记(2015.4.15)

    ThinkPHP __construct()和__initialize() 1.__initialize()不是php类中的函数,php类的构造函数只有__construct().2.类的初始化:子类 ...

  6. OSI七层协议模型及OSI参考模型中的数据封装过程

    转载自:http://blog.csdn.net/qq_14935437/article/details/71081546 OSI模型,即开放式通信系统互联参考模型(Open System Inter ...

  7. [mongodb]child process failed, exited with error number 100

    Run the following command first to start the mongo server mongod run --config /usr/local/etc/mongod. ...

  8. 衡量生活成本:消费者价格指数(CPI, Consumer Price Index)

    经济学家应该如何把经济中的许多价格加总成一个单一指数,从而能够衡量价格的总体水平呢?他们可以简单地计算所有产品与服务价格的平均值,但是这种方法的不足之处是把所有的产品与服务等同处理.由于人们购买的鸡比 ...

  9. Notes of the scrum meeting(12.10)

    meeting time:20:00~20:30p.m.,December 10th,2013 meeting place:20号公寓前 attendees: 顾育豪                  ...

  10. Alpha项目冲刺_博客链接合集

    组员 学号 林泽宇(队长) 211606317 李涵 211606365 尹海川 211606388 郏敏杰 211606307 何永康 211606362 陈炳旭 211606353 苏宇翔 211 ...