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的题目不一样的是要求期望异或和,期望之间 ...
随机推荐
- 爬豆瓣影评,记下解决maximum recursion depth exceeded in cmp
#主要是爬取后给别人做自然语言分析,没其他意思. #coding=utf8 import requests,re from lxml import etree import sys reload(sy ...
- 分享一个有趣的代码,调用电脑中的api语音
在文本文件中输入如下代码: set objTTS = CreateObject("sapi.spvoice") objTTS.speak("为啥我对象这么闹呢?" ...
- 禁用滚动视图ListView、ViewPager、ScrollView、HorizontalScrollView、WebView边界颜色渐变
禁用滚动视图ListView.ViewPager.ScrollView.HorizontalScrollView.WebView边界颜色渐变 ListView.ViewPager.ScrollView ...
- Linux+Redis实战教程_day01_常用命令【重点】
3.常用命令[重点] Linux命令中参数,一般都是无序的.特殊情况下除外 3.1.磁盘管理命令 ls命令:列出目录内容 参数: -a 查询所有文件和文件夹.包含隐藏的 -l 查询详细列表 ls ...
- Web暴力破解--前端JS表单加密进行爆破
0x01 前言 常见的js实现加密的方式有:md5.base64.shal,写了一个简单的demo作为测试. 0x02 代码 login.html <!DOCTYPE HTML> < ...
- Discuz!X 3.4 任意文件删除漏洞复现过程(附python脚本)
今天看下群里在讨论Discuz!X 3.4 任意文件删除漏洞,自己做了一些测试,记录一下过程.结尾附上自己编写的python脚本,自动化实现任意文件删除. 具体漏洞,请查看 https://paper ...
- [Python] Unofficial Windows Binaries for Python Extension Packages
1. Unofficial Windows Binaries for Python Extension Packages 非官方的Python第三方库,提供基于Windows的二进制扩展包,由加州大学 ...
- iOS - 系统权限(关键时刻很有用的)
iOS开发中权限问题: APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事: 那如何提高APP获取iOS系统权限的通过率呢?有以下几种 ...
- hive报错: Specified key was too long; max key length is 767 bytes
废话不多说,报错如下: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while addi ...
- PHP 连接oracle
function connect_oracle(){ static $dbconn = false; if(!$dbconn){ $db_server = "127.0.0.1"; ...