Problem Description
There are N players playing a guessing game. Each player guesses a sequence consists of {1,2,3,4,5,6} with length L, then a dice will be rolled again and again and the roll out sequence will be recorded. The player whose guessing sequence first matches the last L rolls of the dice wins the game.
 
Input
The
first line is the number of test cases. For each test case, the first
line contains 2 integers N (1 ≤ N ≤ 10) and L (1 ≤ L ≤ 10). Each of the
following N lines contains a guessing sequence with length L. It is
guaranteed that the guessing sequences are consist of {1,2,3,4,5,6} and
all the guessing sequences are distinct.
 
Output
For each test case, output a line containing the winning probability of each player with the precision of 6 digits.
 
Sample Input
3
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
 
Sample Output
0.200000 0.200000 0.200000 0.200000
0.200000
0.027778 0.194444 0.194444 0.194444
0.194444 0.194444
0.285337 0.237781 0.237781 0.239102
 

 
对所有串建AC自动机,然后按照Trie图建立递推关系,转化成矩阵,用高斯消元解决带环的转移。
写起来略恶心,反正我是不想调了。
 

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
#define reg register
inline int read() {
int res = ;char ch=getchar();bool fu=;
while(!isdigit(ch))fu|=(ch=='-'),ch=getchar();
while(isdigit(ch)) res=(res<<)+(res<<)+(ch^),ch=getchar();
return fu?-res:res;
} int T;
int n, L;
int nxt[][], fail[], danger[], tot, who[];
inline void ins(int *s, int id)
{
int now = ;
for (reg int i = ; i <= L ; i ++)
now = nxt[now][s[i]] ? nxt[now][s[i]] : nxt[now][s[i]] = ++tot;
danger[now] = ;
who[now] = id;
} inline void Build()
{
queue <int> q;
for (reg int i = ; i <= ;i ++) if (nxt[][i]) q.push(nxt[][i]);
while(!q.empty())
{
int x = q.front();q.pop();
for (reg int i = ; i <= ; i ++)
if (nxt[x][i]) fail[nxt[x][i]] = nxt[fail[x]][i], q.push(nxt[x][i]);
else nxt[x][i] = nxt[fail[x]][i];
danger[x] |= danger[fail[x]];
}
} long double a[][], ans[]; inline void Gauss()
{
for (reg int i = ; i <= tot ; i ++)
{
int k = i;
for (reg int j = i ; j <= tot ; j ++)
if (fabs(a[j][i]) > fabs(a[k][i])) k = j;
if (k != i) swap(a[k], a[i]);
for (reg int j = ; j <= tot ; j ++)
{
if (i == j) continue;
long double r = a[j][i] / a[i][i];
for (reg int k = i ; k <= tot + ; k ++)
a[j][k] -= a[i][k] * r;
}
}
} int main()
{
T = read();
while(T--)
{
memset(nxt, , sizeof nxt);
memset(fail, , sizeof fail);
memset(danger, , sizeof danger);
tot = ;
n = read(), L = read();
int tmp[];
for (reg int i = ; i <= n ; i ++)
{
for (reg int j = ; j <= L ; j ++) tmp[j] = read();
ins(tmp, i);
}
Build();
memset(a, , sizeof a);
for (reg int i = ; i <= tot ; i ++)
{
a[i][i] -= 1.0;
if (danger[i]) continue;
for (reg int j = ; j <= ; j ++)
a[nxt[i][j]][i] += 1.0 / 6.0;
}
a[][tot + ] = -1.0;
// for(int i=0;i<=tot;i++,puts(""))
// for(int j=0;j<=tot+1;j++) printf("%.2Lf ",a[i][j]);
Gauss();
for (reg int i = ; i <= tot ; i ++)
if (danger[i]) ans[who[i]] = a[i][tot + ] / a[i][i];
for (reg int i = ; i < n ; i ++) printf("%.6Lf ", ans[i]);
printf("%.6Lf\n", ans[n]);
}
return ;
}

[HDU5955]Guessing the Dice Roll的更多相关文章

  1. hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】

    含高斯消元模板 2016沈阳区域赛http://acm.hdu.edu.cn/showproblem.php?pid=5955 Guessing the Dice Roll Time Limit: 2 ...

  2. 【AC自动机】【高斯消元】hdu5955 Guessing the Dice Roll

    http://blog.csdn.net/viphong/article/details/53098489 我有一点不是很懂,这样算出来转移到AC自动机根节点的概率是一个远大于1的数. 按我的理解,因 ...

  3. HDU 5955 Guessing the Dice Roll

    HDU 5955 Guessing the Dice Roll 2016 ACM/ICPC 亚洲区沈阳站 题意 有\(N\le 10\)个人,每个猜一个长度为\(L \le 10\)的由\(1-6\) ...

  4. hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】

    hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...

  5. 【HDU5955】Guessing the Dice Roll/马尔科夫

    先从阿里机器学习算法岗网络笔试题说起:甲乙两人进行一个猜硬币的游戏.每个人有一个目标序列,由裁判来抛硬币.谁先得到裁判抛出的一串连续结果,谁赢. 甲的目标序列是正正正,乙的目标序列是反正正.那么如果裁 ...

  6. HDU 5966 Guessing the Dice Roll

    题意有 N≤10 个人,每个猜一个长度为L≤10的由1−6构成的序列,保证序列两两不同.不断地掷骰子,直到后缀与某人的序列匹配,则对应的人获胜.求每个人获胜的概率. 思路:建立trie图,跑高斯消元. ...

  7. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  8. 【HDOJ5955】Guessing the Dice Roll(概率DP,AC自动机,高斯消元)

    题意: 有n个人,每个人有一个长为L的由1~6组成的数串,现在扔一个骰子,依次记录扔出的数字,如果当前扔出的最后L个数字与某个人的数串匹配,那么这个人就算获胜,现在问每个人获胜的概率是多少. n,l& ...

  9. LeetCode 1223. 掷骰子模拟 Dice Roll Simulation - Java - DP

    题目链接:1223. 掷骰子模拟 有一个骰子模拟器会每次投掷的时候生成一个 1 到 6 的随机数. 不过我们在使用它时有个约束,就是使得投掷骰子时,连续 掷出数字 i 的次数不能超过 rollMax[ ...

随机推荐

  1. Hadoop学习笔记之HBase Shell语法练习

    Hadoop学习笔记之HBase Shell语法练习 作者:hugengyong 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令 ...

  2. ubuntu修改中文文件夹名字为英文

    为了使用起来方便,装了ubuntu中文版,自然在home文件里用户目录的“桌面”.“图片”.“视频”.“音乐”……都是中文的. 很多时候都喜欢在桌面上放一些要操作的文件,Linux里命令行操作又多,难 ...

  3. jenkins在windows上自动化部署.Net(.Net Core)项目

    前言 什么是持续集成呢?Continuous integration(CI).持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集成. ...

  4. HttpClient远程接口调用-实名认证

    1.HttpClient远程接口调用 1)用户注册 注册按钮button提交表单时,要return false form表单 <!-- action="http://localhost ...

  5. .ssh/config 文件的解释算法及配置原则

    前言 SSH 是连接远程主机最常用的方式,尽管连接到耽搁主机的基本操作非常直接,但当你开始使用大量的远程系统时,这就会成为笨重和复杂的任务. 幸运的是,OpenSSH 允许您提供自定义的客户端连接选项 ...

  6. 一次五分钟 angularJS (1)—— Binding

    引用angularjs 需要使用AngularJS,需要引用AngularJS的文件 ng-app 要将angular用到页面绑定的时候,我们需要指明它的作用域. 在上图中,ng-app=" ...

  7. webdriver断言

    操作(action).辅助(accessors)和断言(assertion): 操作action: 模拟用户与 Web 应用程序的交互.一般用于操作应用程序的状态. 如点击链接,选择选项的方式进行工作 ...

  8. ubuntu linux 修改ip 超扎心。

    老大说“终于搞定了,快记下来,不然以后又忘了”(露出慈母般的微笑) 参考地址:https://jingyan.baidu.com/article/adc815139ddcc4f723bf7339.ht ...

  9. 蓝松短视频SDK支持AE模板, 可做类似微商视频, 小柿饼的效果等

    AE模板: 是指设计师用Adobe After Effect做好各种视频动画,比如炫酷视频,文艺/搞笑的场景,相册效果等,根据我们的指导文件导出.蓝松SDK会解析导出的文件,自动还原成AE设计时的动画 ...

  10. jQuery常用方法(六)-jQuery 工具

    JQuery Utilities 方法说明 jQuery.browser .msie 表示ie jQuery.browser.version 读取用户浏览器的版本信息 jQuery.boxModel ...