1444: [Jsoi2009]有趣的游戏

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1382  Solved: 498
[Submit][Status][Discuss]

Description

Input

注意 是0<=P

Output

Sample Input

Sample Output

HINT

 30%的数据保证, n ≤ 2. 50%的数据保证, n ≤ 5. 100%的数据保证, n , l, m≤ 10.

Source

析:很容易列出方程,dp[i] = ∑dp[j] * pj ,所以要处理出来就需要AC自动机,然后再用Gauss 消元即可。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. //#define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define all 1,n,1
  35. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  36. #define freopenr freopen("in.txt", "r", stdin)
  37. #define freopenw freopen("out.txt", "w", stdout)
  38. using namespace std;
  39.  
  40. typedef long long LL;
  41. typedef unsigned long long ULL;
  42. typedef pair<int, int> P;
  43. const int INF = 0x3f3f3f3f;
  44. const LL LNF = 1e17;
  45. const double inf = 1e20;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-8;
  48. const int maxn = 150000 + 10;
  49. const int maxm = 3e5 + 10;
  50. const int mod = 10007;
  51. const int dr[] = {-1, 0, 1, 0};
  52. const int dc[] = {0, -1, 0, 1};
  53. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  54. int n, m;
  55. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  56. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. inline bool is_in(int r, int c) {
  58. return r >= 0 && r < n && c >= 0 && c < m;
  59. }
  60. const int maxnode = 10 * 10 + 50;
  61. int sigma;
  62. double A[maxnode][maxnode];
  63. double p[15];
  64. int pos[15];
  65.  
  66. struct Aho{
  67. int ch[maxnode][11], f[maxnode];
  68. bool val[maxnode];
  69. int sz;
  70.  
  71. void init(){ sz = 1; ms(ch[0], 0); }
  72. inline int idx(char ch){ return ch - 'A'; }
  73.  
  74. int insert(const char *s){
  75. int u = 0;
  76. for(int i = 0; s[i]; ++i){
  77. int c = idx(s[i]);
  78. if(!ch[u][c]){
  79. ms(ch[sz], 0);
  80. val[sz] = 0;
  81. ch[u][c] = sz++;
  82. }
  83. u = ch[u][c];
  84. }
  85. val[u] = 1;
  86. return u;
  87. }
  88.  
  89. void getFail(){
  90. queue<int> q; f[0] = 0;
  91. for(int c = 0; c < sigma; ++c){
  92. int u = ch[0][c];
  93. if(u){ q.push(u); f[u] = 0; }
  94. }
  95.  
  96. while(!q.empty()){
  97. int r = q.front(); q.pop();
  98. for(int c = 0; c < sigma; ++c){
  99. int u = ch[r][c];
  100. if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
  101. q.push(u);
  102. int v = f[r];
  103. while(v && !ch[v][c]) v = f[v];
  104. f[u] = ch[v][c];
  105. }
  106. }
  107. }
  108.  
  109. int solve(){
  110. for(int i = 0; i < sz; ++i){
  111. A[i][i] += 1.;
  112. if(val[i]) continue;
  113. for(int j = 0; j < sigma; ++j){
  114. int nxt = ch[i][j];
  115. A[nxt][i] -= p[j];
  116. }
  117. }
  118. return sz;
  119. }
  120. };
  121. Aho aho;
  122. char s[20];
  123.  
  124. void Gauss(int n){
  125. for(int i = 0; i < n; ++i){
  126. int r = i;
  127. for(int j = i+1; j < n; ++j)
  128. if(fabs(A[j][i] > fabs(A[r][i]))) r = j;
  129. if(r != i) for(int j = 0; j <= n; ++j) swap(A[r][j], A[i][j]);
  130.  
  131. for(int k = i+1; k < n; ++k){
  132. double f = A[k][i] / A[i][i];
  133. for(int j = i; j <= n; ++j) A[k][j] -= f * A[i][j];
  134. }
  135. }
  136. for(int i = n-1; i >= 0; --i){
  137. for(int j = i+1; j < n; ++j)
  138. A[i][n] -= A[j][n] * A[i][j];
  139. A[i][n] /= A[i][i];
  140. }
  141. }
  142.  
  143. int main(){
  144. scanf("%d %d %d", &n, &m, &sigma);
  145. for(int i = 0; i < sigma; ++i){
  146. int x, y; scanf("%d %d", &x, &y);
  147. p[i] = x * 1. / y;
  148. }
  149. aho.init();
  150. for(int i = 1; i <= n; ++i){
  151. scanf("%s", s);
  152. pos[i] = aho.insert(s);
  153. }
  154. aho.getFail();
  155. int len = aho.solve();
  156. A[0][len] = 1.;
  157. Gauss(len);
  158. for(int i = 1; i <= n; ++i) printf("%.2f\n", A[pos[i]][len] / A[pos[i]][pos[i]]);
  159. return 0;
  160. }

  

BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)的更多相关文章

  1. BZOJ 1444: [Jsoi2009]有趣的游戏 AC自动机+概率与期望+矩阵乘法

    这道题还比较友好~首先,构建出来 $AC$ 自动机,那么我们要求的就是从 $0$ 号点走无限次走到一个终止节点的概率. 考虑构建转移矩阵 $M,$ $M_{i,j}$ 表示节点 $i$ 转移到节点 $ ...

  2. BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]

    1444: [Jsoi2009]有趣的游戏 题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率 套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩 ...

  3. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

  4. BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)

    诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...

  5. BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法

    题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...

  6. BZOJ 1444:[JSOI2009]有趣的游戏

    BZOJ 1444:[JSOI2009]有趣的游戏 题目链接 首先我们建出Trie图,然后高斯消元. 我们设\(f_i\)表示经过第\(i\)个点的期望次数: \[ f_x=\sum i\cdot p ...

  7. BZOJ:4820: [Sdoi2017]硬币游戏&&BZOJ:1444: [Jsoi2009]有趣的游戏(高斯消元求概率)

    1444: [Jsoi2009]有趣的游戏 4820: [Sdoi2017]硬币游戏 这两道题都是关于不断随机生成字符后求出现给定字符串的概率的问题. 第一题数据范围较小,将串建成AC自动机以后,以A ...

  8. bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】

    https://blog.sengxian.com/solutions/bzoj-1444 orz 一直是我想错了,建出AC自动机之后,实际上这个定义是设f[i]为经过i节点的 * 期望次数 * ,因 ...

  9. BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)

    题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...

随机推荐

  1. EasyUI Dialog 对话框 关闭事件

    在  $('#×××').dialog('close');  执行后触发 $(function(){ $("#titledialos").dialog({ onClose: fun ...

  2. coding利用Webhook实现Push代码后的jenkins自动构建

    安装jenkins 篇:http://www.cnblogs.com/loveyouyou616/p/8714544.html 之前部署了持续集成工具jenkins.通常是开发后的代码先推到 远程代码 ...

  3. go语言websocket使用与客户端html5调用

    我们通过使用如下库创建websocket服务 go get golang.org/x/net/websocket websocket服务端的代码如下: package main; import ( & ...

  4. IE6、7下overflow:hidden失效的问题

    问题产生原因: 当父元素的直接子元素或者下级子元素的样式拥有position:relative或者position:absolute属性时,父元素的overflow:hidden属性就会失效. 例如: ...

  5. Three.js粒子特效,shader渲染初探(一篇非常详细的介绍)

    Three.js粒子特效,shader渲染初探 转载来源:https://juejin.im/post/5b0ace63f265da0db479270a 这大概是个序 关于Three.js,网上有不多 ...

  6. 遇到返回键会退到页面的问题(window.location)

    我的需求是a全局列表页->b展示列表页->c新增页(编辑页)我从b展示列表页,通过编辑进入c编辑页,保存回到b展示列表页. 重,我的b展示列表页,返回要返回的其实是a全局列表页*使用rep ...

  7. ie8,9不支持indexOf解决办法,纯拷贝

    原文在这里,大家快去点啊 自从开始工作后,就没有再碰过原型链了,今天遇到ie8不认识indexOf的时候才发现原型这么嚣张,,哈哈 把代码粘过来,以后留着看 //添加数组IndexOf方法 if (! ...

  8. 19.Mysql优化数据库对象

    19.优化数据库对象19.1 优化表的数据类型应用设计时需要考虑字段的类型和长度,并留有一定长度冗余.procedure analyse()函数可以对表中列的数据类型提出优化建议.procedure ...

  9. Oracle_PL/SQL(6) 触发器(序列、视图)

    序列1.创建序列create sequence seq_alog start with 1 increment by 1 maxvalue 999999999999999999999999999 mi ...

  10. PAT 1071 小赌怡情(15)(代码)

    1071 小赌怡情(15 分) 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算 ...