BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)
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 消元即可。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #include <sstream>
- #include <list>
- #include <assert.h>
- #include <bitset>
- #include <numeric>
- #define debug() puts("++++")
- #define gcd(a, b) __gcd(a, b)
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define fi first
- #define se second
- #define pb push_back
- #define sqr(x) ((x)*(x))
- #define ms(a,b) memset(a, b, sizeof a)
- //#define sz size()
- #define pu push_up
- #define pd push_down
- #define cl clear()
- #define all 1,n,1
- #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const LL LNF = 1e17;
- const double inf = 1e20;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 150000 + 10;
- const int maxm = 3e5 + 10;
- const int mod = 10007;
- const int dr[] = {-1, 0, 1, 0};
- const int dc[] = {0, -1, 0, 1};
- const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline bool is_in(int r, int c) {
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- const int maxnode = 10 * 10 + 50;
- int sigma;
- double A[maxnode][maxnode];
- double p[15];
- int pos[15];
- struct Aho{
- int ch[maxnode][11], f[maxnode];
- bool val[maxnode];
- int sz;
- void init(){ sz = 1; ms(ch[0], 0); }
- inline int idx(char ch){ return ch - 'A'; }
- int insert(const char *s){
- int u = 0;
- for(int i = 0; s[i]; ++i){
- int c = idx(s[i]);
- if(!ch[u][c]){
- ms(ch[sz], 0);
- val[sz] = 0;
- ch[u][c] = sz++;
- }
- u = ch[u][c];
- }
- val[u] = 1;
- return u;
- }
- void getFail(){
- queue<int> q; f[0] = 0;
- for(int c = 0; c < sigma; ++c){
- int u = ch[0][c];
- if(u){ q.push(u); f[u] = 0; }
- }
- while(!q.empty()){
- int r = q.front(); q.pop();
- for(int c = 0; c < sigma; ++c){
- int u = ch[r][c];
- if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
- q.push(u);
- int v = f[r];
- while(v && !ch[v][c]) v = f[v];
- f[u] = ch[v][c];
- }
- }
- }
- int solve(){
- for(int i = 0; i < sz; ++i){
- A[i][i] += 1.;
- if(val[i]) continue;
- for(int j = 0; j < sigma; ++j){
- int nxt = ch[i][j];
- A[nxt][i] -= p[j];
- }
- }
- return sz;
- }
- };
- Aho aho;
- char s[20];
- void Gauss(int n){
- for(int i = 0; i < n; ++i){
- int r = i;
- for(int j = i+1; j < n; ++j)
- if(fabs(A[j][i] > fabs(A[r][i]))) r = j;
- if(r != i) for(int j = 0; j <= n; ++j) swap(A[r][j], A[i][j]);
- for(int k = i+1; k < n; ++k){
- double f = A[k][i] / A[i][i];
- for(int j = i; j <= n; ++j) A[k][j] -= f * A[i][j];
- }
- }
- for(int i = n-1; i >= 0; --i){
- for(int j = i+1; j < n; ++j)
- A[i][n] -= A[j][n] * A[i][j];
- A[i][n] /= A[i][i];
- }
- }
- int main(){
- scanf("%d %d %d", &n, &m, &sigma);
- for(int i = 0; i < sigma; ++i){
- int x, y; scanf("%d %d", &x, &y);
- p[i] = x * 1. / y;
- }
- aho.init();
- for(int i = 1; i <= n; ++i){
- scanf("%s", s);
- pos[i] = aho.insert(s);
- }
- aho.getFail();
- int len = aho.solve();
- A[0][len] = 1.;
- Gauss(len);
- for(int i = 1; i <= n; ++i) printf("%.2f\n", A[pos[i]][len] / A[pos[i]][pos[i]]);
- return 0;
- }
BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)的更多相关文章
- BZOJ 1444: [Jsoi2009]有趣的游戏 AC自动机+概率与期望+矩阵乘法
这道题还比较友好~首先,构建出来 $AC$ 自动机,那么我们要求的就是从 $0$ 号点走无限次走到一个终止节点的概率. 考虑构建转移矩阵 $M,$ $M_{i,j}$ 表示节点 $i$ 转移到节点 $ ...
- BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]
1444: [Jsoi2009]有趣的游戏 题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率 套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩 ...
- 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法
[BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT 30%的 ...
- BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)
诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...
- 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 ...
- BZOJ 1444:[JSOI2009]有趣的游戏
BZOJ 1444:[JSOI2009]有趣的游戏 题目链接 首先我们建出Trie图,然后高斯消元. 我们设\(f_i\)表示经过第\(i\)个点的期望次数: \[ f_x=\sum i\cdot p ...
- BZOJ:4820: [Sdoi2017]硬币游戏&&BZOJ:1444: [Jsoi2009]有趣的游戏(高斯消元求概率)
1444: [Jsoi2009]有趣的游戏 4820: [Sdoi2017]硬币游戏 这两道题都是关于不断随机生成字符后求出现给定字符串的概率的问题. 第一题数据范围较小,将串建成AC自动机以后,以A ...
- bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】
https://blog.sengxian.com/solutions/bzoj-1444 orz 一直是我想错了,建出AC自动机之后,实际上这个定义是设f[i]为经过i节点的 * 期望次数 * ,因 ...
- BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)
题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...
随机推荐
- EasyUI Dialog 对话框 关闭事件
在 $('#×××').dialog('close'); 执行后触发 $(function(){ $("#titledialos").dialog({ onClose: fun ...
- coding利用Webhook实现Push代码后的jenkins自动构建
安装jenkins 篇:http://www.cnblogs.com/loveyouyou616/p/8714544.html 之前部署了持续集成工具jenkins.通常是开发后的代码先推到 远程代码 ...
- go语言websocket使用与客户端html5调用
我们通过使用如下库创建websocket服务 go get golang.org/x/net/websocket websocket服务端的代码如下: package main; import ( & ...
- IE6、7下overflow:hidden失效的问题
问题产生原因: 当父元素的直接子元素或者下级子元素的样式拥有position:relative或者position:absolute属性时,父元素的overflow:hidden属性就会失效. 例如: ...
- Three.js粒子特效,shader渲染初探(一篇非常详细的介绍)
Three.js粒子特效,shader渲染初探 转载来源:https://juejin.im/post/5b0ace63f265da0db479270a 这大概是个序 关于Three.js,网上有不多 ...
- 遇到返回键会退到页面的问题(window.location)
我的需求是a全局列表页->b展示列表页->c新增页(编辑页)我从b展示列表页,通过编辑进入c编辑页,保存回到b展示列表页. 重,我的b展示列表页,返回要返回的其实是a全局列表页*使用rep ...
- ie8,9不支持indexOf解决办法,纯拷贝
原文在这里,大家快去点啊 自从开始工作后,就没有再碰过原型链了,今天遇到ie8不认识indexOf的时候才发现原型这么嚣张,,哈哈 把代码粘过来,以后留着看 //添加数组IndexOf方法 if (! ...
- 19.Mysql优化数据库对象
19.优化数据库对象19.1 优化表的数据类型应用设计时需要考虑字段的类型和长度,并留有一定长度冗余.procedure analyse()函数可以对表中列的数据类型提出优化建议.procedure ...
- Oracle_PL/SQL(6) 触发器(序列、视图)
序列1.创建序列create sequence seq_alog start with 1 increment by 1 maxvalue 999999999999999999999999999 mi ...
- PAT 1071 小赌怡情(15)(代码)
1071 小赌怡情(15 分) 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算 ...