HDU 5966 Guessing the Dice Roll
题意
有 N≤10 个人,每个猜一个长度为L≤10的由1−6构成的序列,保证序列两两不同。
不断地掷骰子,直到后缀与某人的序列匹配,则对应的人获胜。
求每个人获胜的概率。
思路:
建立trie图,跑高斯消元.
高斯消元每个点的意义是:
第i行第j列的值为x 有概率x从点j转移过来
const double eps = 1e-;
const int SIGMA_SIZE = ;
const int MAXNODE = ; int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE];
int sz; void insert(int* P, int len, int v) {
int u = ;
for (int i = ; i < len; i++) {
int c = P[i];
if (!ch[u][c]) {
memset(ch[sz], , sizeof(ch[sz]));
ch[u][c] = sz++;
}
u = ch[u][c];
}
} void getFail() {
f[] = ;
queue<int> q;
for (int c = ; c < SIGMA_SIZE; c++) {
int u = ch[][c];
if (u) {
q.push(u);
f[u] = ;
}
}
while (!q.empty()) {
int r = q.front(); q.pop();
for (int c = ; c < SIGMA_SIZE; 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];
}
}
} //double的高斯消元
double a[MAXNODE][MAXNODE], x[MAXNODE];//方程的左边的矩阵和等式右边的值,求解之后x存的就是结果
int equ, var;//方程数和未知数个数
int Gauss()
{
for (int k=, col=; k<equ && col<var; k++, col++)
{
int max_r=k;
for (int 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 (int j = col; j < var; j++) swap(a[k][j], a[max_r][j]);
swap(x[k], x[max_r]);
}
x[k] /= a[k][col];
for (int j=col+; j<var; j++) a[k][j] /= a[k][col];
a[k][col] = ;
for (int i=; i<equ; i++)
if (i != k)
{
x[i] -= x[k] * a[i][k];
for(int j=col+; j<var; j++) a[i][j]-=a[k][j]*a[i][col];
a[i][col] = ;
}
}
return ;
} const int maxn = ;
int n, len;
int peo[maxn][maxn];
bool end[MAXNODE];
vector<int> G[MAXNODE]; void init()
{
memset(f, , sizeof(f));
memset(ch, , sizeof(ch));
memset(a, , sizeof(a));
memset(end, , sizeof(end));
memset(x, , sizeof(x)); sz = ;
scanf("%d%d", &n, &len);
for (int i = ; i <= n; i++)
{
for (int j = ; j < len; j++)
{
scanf("%d", &peo[i][j]);
}
insert(peo[i], len, i);
end[sz - ] = true;
}
for (int i = ; i < sz; i++)
{
G[i].clear();
}
} void getGraph()
{
getFail();
for (int i = ; i < sz; i++)
{
if (!end[i])
{
for (int j = ; j < ; j++)
{
int t = ch[i][j];
G[t].push_back(i);
}
}
}
} void solve()
{
getGraph();
equ = var = sz;
for (int i = ; i <sz; i++)
{
for (auto j : G[i])
{
a[i][j] += 1.0/;
}
a[i][i] -= ;
}
x[] = -; Gauss(); vector<double> ans;
for (int i = ; i < sz; i++)
{
if (end[i])
{
ans.push_back(x[i]);
}
}
for (int i = ; i < ans.size() - ; i++)
{
printf("%.6f ", ans[i]);
}
printf("%.6f\n", *ans.rbegin()); } int main()
{
int T;
scanf("%d", &T);
while (T--)
{
init();
solve();
}
return ;
}
HDU 5966 Guessing the Dice Roll的更多相关文章
- HDU 5955 Guessing the Dice Roll
HDU 5955 Guessing the Dice Roll 2016 ACM/ICPC 亚洲区沈阳站 题意 有\(N\le 10\)个人,每个猜一个长度为\(L \le 10\)的由\(1-6\) ...
- hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】
hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...
- hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】
含高斯消元模板 2016沈阳区域赛http://acm.hdu.edu.cn/showproblem.php?pid=5955 Guessing the Dice Roll Time Limit: 2 ...
- 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 ...
- [HDU5955]Guessing the Dice Roll
Problem Description There are N players playing a guessing game. Each player guesses a sequence cons ...
- 【HDU5955】Guessing the Dice Roll/马尔科夫
先从阿里机器学习算法岗网络笔试题说起:甲乙两人进行一个猜硬币的游戏.每个人有一个目标序列,由裁判来抛硬币.谁先得到裁判抛出的一串连续结果,谁赢. 甲的目标序列是正正正,乙的目标序列是反正正.那么如果裁 ...
- 【AC自动机】【高斯消元】hdu5955 Guessing the Dice Roll
http://blog.csdn.net/viphong/article/details/53098489 我有一点不是很懂,这样算出来转移到AC自动机根节点的概率是一个远大于1的数. 按我的理解,因 ...
- 【HDOJ5955】Guessing the Dice Roll(概率DP,AC自动机,高斯消元)
题意: 有n个人,每个人有一个长为L的由1~6组成的数串,现在扔一个骰子,依次记录扔出的数字,如果当前扔出的最后L个数字与某个人的数串匹配,那么这个人就算获胜,现在问每个人获胜的概率是多少. n,l& ...
- hdu 4586 Play the Dice 概率推导题
A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
随机推荐
- app启动时间命令
app启动: 冷启动和热启动 冷启动方式: adb shell am start -W -n package/activity 停止app命令: adb shell am force-stop pac ...
- Ubuntu下freeradius-server的安装与mysql-server的关联
1.创建freeradius数据库 #service mysql start ... #mysql -u root -p Enter password: 456456 ... mysql> cr ...
- EXCEL里面的数字显示为文本 不用科学计数法显示
1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可. 已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...
- 19:A*B问题
总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个正整数A和B,求A*B. 输入 一行,包含两个正整数A和B,中间用单个空格隔开.1 <= A,B <= 50000 ...
- Android 数据库管理— — —更新数据
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- C语言程序设计第十次作业
一.实验内容 1.有5名学生,每名学生有语文.数学和外语3门课的考试成绩.编程统计各学生的总分和平均分以及所有学生各科的平均分.要求成绩在程序中初始化,结果以表格的形式输出. ...
- window10的优缺点
windows10的体验随笔 为了体验科技前沿,前段时间升级了windows10 优点: 首先换了windows10就回不去了, 1.开始菜单的回归是众向所归,而且也加了迷你的一些菜单元素,值得称 ...
- SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException: xxx
刚配置hadoop2.2,格式化namenode时候报的这个错. 原因是hadoop在格式化HDFS的时候,通过hostname命令获取到的主机名在/etc/hosts文件中进行映射的时候,没有找到, ...
- 基础小功能之(1)震动,(2)检测app是否在前台运行
//开启震动 //添加权限<uses-permission android:name="android.permission.VIBRATE" /> private v ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...