hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】
含高斯消元模板
2016沈阳区域赛http://acm.hdu.edu.cn/showproblem.php?pid=5955
Guessing the Dice Roll
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1632 Accepted Submission(s): 480
5 1
1
2
3
4
5
6 2
1 1
2 1
3 1
4 1
5 1
6 1
4 3
1 2 3
2 3 4
3 4 5
4 5 6
0.200000
0.027778 0.194444 0.194444 0.194444
0.194444 0.194444
0.285337 0.237781 0.237781 0.239102
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535 int n, l, t;
const int maxn = ;
struct trie{
int son[];
int ed;
int fail;
}AC[maxn];
int tot = ;
int fp[]; void build(int s[], int id)
{
int now = ;
for(int i = ; i < l; i++){
if(AC[now].son[s[i]] == ){
AC[now].son[s[i]] = ++tot;
}
now = AC[now].son[s[i]];
}
AC[now].ed = id;
fp[id] = now;
} void get_fail()
{
queue<int>que;
for(int i = ; i <= ; i++){
if(AC[].son[i] != ){
AC[AC[].son[i]].fail = ;
que.push(AC[].son[i]);
}
} while(!que.empty()){
int u = que.front();
que.pop();
for(int i = ; i <= ; i++){
if(AC[u].son[i] != ){
AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
que.push(AC[u].son[i]);
}
else{
AC[u].son[i] = AC[AC[u].fail].son[i];
}
}
}
} double a[maxn][maxn], x[maxn];
const double eps = 1e-;
int equ, var;
void gauss()
{
equ = var = tot + ;
int i,j,k,col,max_r;
for(k=,col=;k<equ&&col<var;k++,col++)
{
max_r=k;
for(i=k+;i<equ;i++)
{
if(fabs(a[i][col] )>fabs(a[max_r][col] ) ) max_r=i;
}
if(fabs(a[max_r][col])<eps ) return;
if(k!=max_r)
{
for(j=col;j<=var;j++) swap(a[k][j],a[max_r][j] );
}
for(j=col+;j<=var;j++) a[k][j]/=a[k][col]; a[k][col]=; for(i=;i<equ;i++) if(i!=k)
{
for(j=col+;j<=var;j++) a[i][j]-=a[k][j]*a[i][col]; a[i][col]=;
}
}
for(i=;i<equ;i++) x[i]=a[i][var];
return;
} int main()
{
scanf("%d", &t);
while(t--){
for(int i = ; i <= tot; i++){
AC[i].fail = ;
AC[i].ed = ;
for(int j = ; j < ; j++){
AC[i].son[j] = ;
}
}
tot = ; scanf("%d%d", &n, &l);
for(int i = ; i <= n; i++){
int tmp[];
for(int j = ; j < l; j++){
scanf("%d", &tmp[j]);
}
build(tmp, i);
}
get_fail(); memset(a, , sizeof(a));
memset(x, , sizeof(x));
for(int i = ; i <= tot; i++)a[i][i] = -1.0;
for(int i = ; i <= tot; i++){
if(AC[i].ed == ){
for(int j = ; j <= ; j++){
int to = AC[i].son[j];
a[to][i] += 1.0 / ;
}
} } a[][tot + ] = -1.0;//虚拟节点
gauss();
for(int i = ; i <= n; i++){
printf("%.6f", x[fp[i]]);
if(i == n){
printf("\n");
}
else{
printf(" ");
}
} //cout<<"yes"<<endl;
} return ;
}
hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】的更多相关文章
- hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】
hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...
- UVALive - 3490 Generator (AC自动机+高斯消元dp)
初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...
- BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]
1444: [Jsoi2009]有趣的游戏 题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率 套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩 ...
- [HDU5955]Guessing the Dice Roll
Problem Description There are N players playing a guessing game. Each player guesses a sequence cons ...
- [JLOI2012]时间流逝 树上高斯消元 概率期望
题面 题意:(感觉题面写的题意是错的?)有\(n\)种能量不同的圈,设当前拥有的圈的集合为\(S\),则: 1,每天有\(p\)概率失去一个能量最小的圈.特别的,如果\(S = \varnothing ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- 【BZOJ-3270】博物馆 高斯消元 + 概率期望
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 [高斯消元 概率DP]
1778: [Usaco2010 Hol]Dotp 驱逐猪猡 题意:一个炸弹从1出发p/q的概率爆炸,否则等概率走向相邻的点.求在每个点爆炸的概率 高斯消元求不爆炸到达每个点的概率,然后在一个点爆炸就 ...
- BZOJ 2337: [HNOI2011]XOR和路径 [高斯消元 概率DP]
2337: [HNOI2011]XOR和路径 题意:一个边权无向连通图,每次等概率走向相连的点,求1到n的边权期望异或和 这道题和之前做过的高斯消元解方程组DP的题目不一样的是要求期望异或和,期望之间 ...
随机推荐
- 基于NDK的Android防破解& Android防破解 【转载】
两篇防破解文章转载 基于NDK的Android防破解:http://blog.csdn.net/bugrunner/article/details/8634585 Android防破解:http:// ...
- 用JAVA发送一个XML格式的HTTP请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- redis 列表
Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...
- php-fpm 配置进程池
什么是 php-fpm :php 是作为一个独立服务存在的,这个服务叫做 php-fpm什么是 php-fpm pool :也就是 php-fpm 的进程池,这个进程池中运行了多个子进程,用来并发处理 ...
- Selenium 前进和后退
我们平常使用浏览器时都有前进和后退功能, Selenium 也可以完成这个操作,它使用 back() 方法后退,使用 forward() 方法前进 from selenium import webdr ...
- Android分包原理
如果App引用的库太多,方法数超过65536后无法编译.这是因为单个dex里面不能有超过65536个方法.为什么有最大的限制呢,因为Android会把每一个类的方法id检索起来,存在一个链表结构里面. ...
- React Native汇错归纳(持续更新中……)
1.2017-10-25: 报错信息:“Cannot find entry file index.android.js in any of roots…..” 解决方法: 1.首先从虚拟机中找问题:看 ...
- Java接口回调机制
一.前言 最近在看android Fragment与Activity进行数据传递的部分,看到了接口回调的内容,今天来总结一下. 二.回调的含义和用途 1.什么是回调? 一般来说,模块之间都存在一定的调 ...
- audio_policy.conf说明(翻译)
自己记着当笔记,水平有限,仅供参考 # # Audio policy configuration for generic device builds (goldfish audio HAL - emu ...
- 解决Sublime Text 3中文显示乱码问题(转)
解决Sublime Text 3中文显示乱码问题(转) 1.打开Sublime Text 3,按Ctrl+-打开控制行,复制粘贴以下python代码,然后回车运行. 2. 复制并粘贴如下代码: 2.重 ...