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

题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率。

题解:裸的AC自动机,求匹配到终止结点的概率,用 高斯消元?一开始不知道怎么建方程组,直接举个例子吧:

Input:

1

2 2

1 1

2 1

图解:

x0原本概率就是1,然后还要加上其他结点走向它的概率,,这样最后算下来是大于1的,现在还是觉得怪怪的。。。

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
const int N = ;
const int M = ;
int equ, var;
double a[N][N];
double x[N];
void Gauss() {
int i, j, k;
int max_r;
int col;
for(col = , k = ; 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(max_r != k) {
for(j = k; j < var; ++j)
swap(a[k][j], a[max_r][j]);
swap(x[k], x[max_r]);
}
x[k] /= a[k][col];
for(j = col+; j < var; ++j)
a[k][j] /= a[k][col];
a[k][col] = ;
for(i = ; i < equ; ++i) {
if(i != k) {
x[i] -= x[k] * a[i][col];
for(j = col+; j < var; ++j)
a[i][j] -= a[k][j] * a[i][col];
a[i][col] = ;
}
}
}
}
int mk[N];
struct Trie {
int next[N][M], fail[N], tag[N];
int root, L;
int newnode() {
for(int i = ;i < M;i++)
next[L][i] = -;
tag[L++] = ;
return L-;
}
void init() { L = ; root = newnode(); }
void Insert(int buf[], int len, int id) {
int u = root;
for(int i = ; i < len; i++) {
if(next[u][buf[i]] == -)
next[u][buf[i]] = newnode();
u = next[u][buf[i]];
}
tag[u] = ;
mk[id] = u;
}
void build() {
queue<int> Q;
fail[root] = root;
for(int i = ; i < M; i++) {
if(next[root][i] == -)
next[root][i] = root;
else {
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while( !Q.empty() ) {
int u = Q.front();
Q.pop();
for(int i = ; i < M; i++) {
int &v = next[u][i];
if(v == -)
v = next[fail[u]][i];
else {
Q.push(v);
fail[v] = next[fail[u]][i];
}
}
}
}
void query(int n) {
int i , j;
CLR(a, ); CLR(x, );
equ = var = L;
for(i = ; i < L; ++i) a[i][i] = -1.0;
x[] = -1.0;
for(i = ; i < L; ++i) {
for(j = ; j < M; ++j) {
if(next[i][j] != - && tag[i]==)
a[next[i][j]][i] += 1.0/;
}
}
Gauss();
for(i = ; i < n-; ++i)
printf("%.6f ", x[mk[i]]);
printf("%.6f\n", x[mk[n-]]);
}
};
Trie ac;
int main() {
int T, n, m;
int A[];
scanf("%d",&T);
while( T-- ) {
scanf("%d%d", &n, &m);
ac.init();
for(int i = ; i < n; i++) {
for(int j = ; j < m; ++j) {
scanf("%d", &A[j]);
A[j]--; //0~5
}
ac.Insert(A, m, i);
}
ac.build();
ac.query(n);
}
return ;
}

hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】的更多相关文章

  1. HDU 5955 Guessing the Dice Roll

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

  2. UVALive - 3490 Generator (AC自动机+高斯消元dp)

    初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...

  3. BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]

    1444: [Jsoi2009]有趣的游戏 题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率 套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩 ...

  4. HDU 2262 Where is the canteen 期望dp+高斯消元

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2262 Where is the canteen Time Limit: 10000/5000 MS ...

  5. HDU 5966 Guessing the Dice Roll

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

  6. 【HDU 3949】 XOR (线性基,高斯消元)

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. hdu 5833 Zhu and 772002 异或方程组高斯消元

    ccpc网赛卡住的一道题 蓝书上的原题 但是当时没看过蓝书 今天又找出来看看 其实也不是特别懂 但比以前是了解了一点了 主要还是要想到构造异或方程组 异或方程组的消元只需要xor就好搞了 数学真的是硬 ...

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

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

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

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

随机推荐

  1. CMS gc随记

    在查看CMS相关中文资料时,都提到了 并发预清理(Concurrent precleaning) 重新标记(STW remark) 目的是重新标记在并发标记阶段,由于对象状态的改变而标记遗漏的对象. ...

  2. Javaee的Dao层的抽取

    有时候我们在实现不同功能的时候回看到很多的Dao层的增加.修改.删除.查找都很相似,修改我们将他们提取BaseDao 一.提取前 1. 提取前的LinkDao层: public interface L ...

  3. mac os下载安装jmeter

    一.简介 jmeter是属于apache的一个开源产品,纯Java应用.最初用来进行功能测试,而后又扩展了更多的测试功能. 二.下载 进入apache的jmeter下载页:http://jmeter. ...

  4. Java中的断言 Assert

    今天正好遇到了,就记一下 一.作用: 用与编写单元测试 二.assert 关键字 assert 理论上和 if类似, 但是assert 仅仅用于测试, 不能用于业务 如果发现断言无效, 则可能时ide ...

  5. Boxlayout中button改变大小

    需要先设置maximunsize neuStart.setBorder(BorderFactory.createRaisedBevelBorder()); neuStart.setMaximumSiz ...

  6. 一、linux下安装redis(单机)

    1.下载redis,http://download.redis.io/releases/redis-3.2.7.tar.gz 2.linux我用的是centos6.5 3.把redis上传到cento ...

  7. react-native一些好的组件

    一.移动端路由 react-navigator 二.移动端本地储存 react-native-storage(https://github.com/sunnylqm/react-native-stor ...

  8. Create a soft keyboard

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. JS判断请求来自Android手机还是iPhone手机

    <script type="text/javascript"> var browser = { versions: function () { var u = navi ...

  10. javascript判断一个元素是另一个元素的子元素

    function isParent (obj,parentObj){ while (obj != undefined && obj != null && obj.tag ...