1444: [Jsoi2009]有趣的游戏

链接

分析:

  如果一个点回到0号点,那么会使0号点的概率增加,而0号点的概率本来是1,不能增加,所以这题用期望做。

  设$x_i$表示经过i的期望次数,然后初始可以知道$x_0=0$,又因为末尾节点只会经过一次,所以末尾节点的概率就是期望。

  然后建出AC自动机,高斯消元。

  参考sengxian

代码:

Gauss

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const double eps = 1e-;
int ch[N][], val[N], fail[N], id[N], q[N], Index;
int n, L, m;
char s[N];
double A[N][N], p[N]; void Insert(int x) {
int now = ;
for (int i = ; i < L; ++i) {
int c = s[i] - 'A';
if (!ch[now][c]) ch[now][c] = ++Index;
now = ch[now][c];
}
val[now] = , id[x] = now;
}
void bfs() {
int L = , R = ;
for (int c = ; c < m; ++c) if (ch[][c]) q[++R] = ch[][c];
while (L <= R) {
int u = q[L ++];
for (int c = ; c < m; ++c) {
int v = ch[u][c];
if (!v) ch[u][c] = ch[fail[u]][c];
else {
fail[v] = ch[fail[u]][c];
val[v] |= val[fail[v]];
q[++R] = v;
}
}
}
}
bool Gauss(int n) {
for (int k = ; k <= n; ++k) {
int r = k;
for (int i = k + ; i <= n; ++i) if (A[i][k] > A[r][k]) r = k;
if (r != k) for (int j = ; j <= n + ; ++j) swap(A[r][j], A[k][j]);
for (int i = k + ; i <= n; ++i) {
if (fabs(A[i][k]) > eps) {
double t = A[i][k] / A[k][k];
for (int j = ; j <= n + ; ++j) A[i][j] -= A[k][j] * t;
}
}
}
for (int i = n; i >= ; --i) {
for (int j = i + ; j <= n; ++j) A[i][n + ] -= A[j][n + ] * A[i][j];
A[i][n + ] /= A[i][i];
}
return ;
}
int main() {
n = read(), L = read(), m = read();
for (int i = ; i < m; ++i) {
int u = read(), v = read();
p[i] = 1.0 * u / v;
}
int cnt = ;
for (int i = ; i < n; ++i) {
scanf("%s", s);
for (int j = ; j < L; ++j)
if (p[s[j] - 'A'] <= eps) { cnt ++; break; }
Insert(i);
}
if (cnt == n) {
for (int i = ; i <= n; ++i) puts("0.00"); return ;
}
bfs();
A[][Index + ] = -;
for (int i = ; i <= Index; ++i) {
A[i][i] = -1.0;
if (val[i]) continue;
for (int c = ; c < m; ++c) A[ch[i][c]][i] += p[c];
}
Gauss(Index);
for (int i = ; i < n; ++i) {
double p = A[id[i]][Index + ];
if (fabs(p) <= eps) puts("0.00");
else printf("%.2lf\n", p);
}
return ;
}

迭代+矩阵

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
struct Node {
double a[N][N];
Node() { memset(a, , sizeof(a)); }
} A;
int ch[N][], val[N], fail[N], id[N], q[N], Index;
int n, L, m;
char s[N];
double p[N]; Node operator * (const Node &A,const Node &B) {
Node C;
for (int k = ; k <= Index; ++k)
for (int i = ; i <= Index; ++i)
for (int j = ; j <= Index; ++j)
C.a[i][j] += A.a[i][k] * B.a[k][j];
return C;
}
void Insert(int x) {
int now = ;
for (int i = ; i < L; ++i) {
int c = s[i] - 'A';
if (!ch[now][c]) ch[now][c] = ++Index;
now = ch[now][c];
}
val[now] = , id[x] = now;
}
void bfs() {
int L = , R = ;
for (int c = ; c < m; ++c) if (ch[][c]) q[++R] = ch[][c];
while (L <= R) {
int u = q[L ++];
for (int c = ; c < m; ++c) {
int v = ch[u][c];
if (!v) ch[u][c] = ch[fail[u]][c];
else {
fail[v] = ch[fail[u]][c];
val[v] |= val[fail[v]];
q[++R] = v;
}
}
}
}
int main() {
n = read(), L = read(), m = read();
for (int i = ; i < m; ++i) {
int u = read(), v = read();
p[i] = 1.0 * u / v;
}
for (int i = ; i < n; ++i) {
scanf("%s", s);
Insert(i);
}
bfs();
for (int i = ; i <= Index; ++i) {
if (val[i]) A.a[i][i] = ;
else for (int c = ; c < m; ++c) A.a[i][ch[i][c]] += p[c];
}
for (int i = ; i <= ; ++i) A = A * A;
for (int i = ; i < n; ++i) printf("%.2lf\n", A.a[][id[i]]);
return ;
}

1444: [Jsoi2009]有趣的游戏的更多相关文章

  1. BZOJ:4820: [Sdoi2017]硬币游戏&&BZOJ:1444: [Jsoi2009]有趣的游戏(高斯消元求概率)

    1444: [Jsoi2009]有趣的游戏 4820: [Sdoi2017]硬币游戏 这两道题都是关于不断随机生成字符后求出现给定字符串的概率的问题. 第一题数据范围较小,将串建成AC自动机以后,以A ...

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

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

  3. BZOJ 1444:[JSOI2009]有趣的游戏

    BZOJ 1444:[JSOI2009]有趣的游戏 题目链接 首先我们建出Trie图,然后高斯消元. 我们设\(f_i\)表示经过第\(i\)个点的期望次数: \[ f_x=\sum i\cdot p ...

  4. BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)

    1444: [Jsoi2009]有趣的游戏 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1382  Solved: 498[Submit][Statu ...

  5. ●BZOJ 1444 [Jsoi2009]有趣的游戏

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1444题解.1: 概率dp,矩阵乘法,快速幂. 对所有串建立AC自动机, 那么如果在trie树 ...

  6. bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】

    https://blog.sengxian.com/solutions/bzoj-1444 orz 一直是我想错了,建出AC自动机之后,实际上这个定义是设f[i]为经过i节点的 * 期望次数 * ,因 ...

  7. BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)

    诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...

  8. BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)

    题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...

  9. BZOJ 1444: [Jsoi2009]有趣的游戏 AC自动机+概率与期望+矩阵乘法

    这道题还比较友好~首先,构建出来 $AC$ 自动机,那么我们要求的就是从 $0$ 号点走无限次走到一个终止节点的概率. 考虑构建转移矩阵 $M,$ $M_{i,j}$ 表示节点 $i$ 转移到节点 $ ...

随机推荐

  1. springMVC入门-03

    接着上一讲介绍springMVC针对rest风格的支持. 查询数据 使用前:/user_show?id=120 使用后:/user/120 删除数据 使用前:/user_delete?id=123 使 ...

  2. 使用Percona MySQL 5.7版本遇到的坑

    监控DB由于使用的TokuDB引擎,因此选择使用Percona MySQL 5.7版本,在使用过程中遇到了比较多的坑,在这里做一下简单的记录,希望对广大DBA有帮助. load文件飙升导致的DB雪崩 ...

  3. Python日志记录(logging)

    import logging logfile = 'e:\\a.txt' # logging.basicConfig(filename=logfile,level=logging.INFO) # lo ...

  4. 多台服务器共享session问题

    在现在的大型网站中,如何实现多台服务器中的session数据共享呢 当使用多台服务器架设成集群之后,我们通过负载均衡的方式,同一个用户(或者ip)访问时被分配到不同的服务器上,假设在A服务器登录,如果 ...

  5. yii 验证码功能的实现

    首先知晓我们在使用验证码的时候通常是和我们的表单小部件配合使用首先我们创建model层 新建一个php文件 名字叫做Verifycode.php 要在我们的model层 创建我们的验证码的验证规则,我 ...

  6. Jmeter入门3 http请求—content-type与参数

    本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...

  7. BZOJ 1066 蜥蜴 最大流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1066 题目大意: 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥 ...

  8. 【洛谷】【搜索(dfs)】P1363 幻想迷宫

    [题目描述:] 幻象迷宫可以认为是无限大的,不过它由若干个N*M的矩阵重复组成.矩阵中有的地方是道路,用'.'表示:有的地方是墙,用'#'表示.LHX和WD所在的位置用'S'表示.也就是对于迷宫中的一 ...

  9. python伪装网页访问

    # -*- coding:utf8 -*-#import urllib.request#url =' http://www.douban.com/'#webPage=urllib.request.ur ...

  10. PHP类的静态(static)方法和静态(static)变量使用介绍

    PHP类的静态(static)方法和静态(static)变量使用介绍,学习php的朋友可以看下     在php中,访问类的方法/变量有两种方法: 1. 创建对象$object = new Class ...