1444: [Jsoi2009]有趣的游戏
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]有趣的游戏的更多相关文章
- BZOJ:4820: [Sdoi2017]硬币游戏&&BZOJ:1444: [Jsoi2009]有趣的游戏(高斯消元求概率)
1444: [Jsoi2009]有趣的游戏 4820: [Sdoi2017]硬币游戏 这两道题都是关于不断随机生成字符后求出现给定字符串的概率的问题. 第一题数据范围较小,将串建成AC自动机以后,以A ...
- BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]
1444: [Jsoi2009]有趣的游戏 题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率 套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩 ...
- BZOJ 1444:[JSOI2009]有趣的游戏
BZOJ 1444:[JSOI2009]有趣的游戏 题目链接 首先我们建出Trie图,然后高斯消元. 我们设\(f_i\)表示经过第\(i\)个点的期望次数: \[ f_x=\sum i\cdot p ...
- BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)
1444: [Jsoi2009]有趣的游戏 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1382 Solved: 498[Submit][Statu ...
- ●BZOJ 1444 [Jsoi2009]有趣的游戏
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1444题解.1: 概率dp,矩阵乘法,快速幂. 对所有串建立AC自动机, 那么如果在trie树 ...
- bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】
https://blog.sengxian.com/solutions/bzoj-1444 orz 一直是我想错了,建出AC自动机之后,实际上这个定义是设f[i]为经过i节点的 * 期望次数 * ,因 ...
- BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)
诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...
- BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)
题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...
- BZOJ 1444: [Jsoi2009]有趣的游戏 AC自动机+概率与期望+矩阵乘法
这道题还比较友好~首先,构建出来 $AC$ 自动机,那么我们要求的就是从 $0$ 号点走无限次走到一个终止节点的概率. 考虑构建转移矩阵 $M,$ $M_{i,j}$ 表示节点 $i$ 转移到节点 $ ...
随机推荐
- 一些简单的SQL Server服务器监控
1:磁盘活动的一些监控 指标 吞吐量:IOPS,存储子系统每秒能提供多少次I/O 吞吐量,MB/S,I/O子系统每秒能提供多少MB 延时:每个I/O请求占用多少时间 队列长度:队列中有多少IO请求在等 ...
- 缓存知识整理(包含Redis)
一.缓存知识 1.buffer和cache的区别 Buffer 缓冲 写操作 写缓冲 Cache 缓存 读操作 读缓存 磁盘-->内存-->CPU 2.PHP的缓存方案 官方文档:h ...
- mysql的表和数据类型
一.查看当前数据库所有表 mysql> use db Database changed mysql> show tables; Empty set (0.00 sec) #表示db数据库下 ...
- September 09th 2017 Week 36th Saturday
Don't wait to be lonely, to recognize the value of a friend. 不要等到孤独了,才明白朋友的价值. Don't wait to be left ...
- 在web.xml中配置404错误拦截
<error-page> <error-code>404</error-code> <location>/home.do</location> ...
- S1 商品信息管理系统
#include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> ...
- CSS3新特性2D、3D效果讲解
希望这篇博客可以对你有所帮助,如果有什么技术上的问题,希望我们可以做进一步的交流,如果你觉得我哪里阐述的不正确或者你有更好的更透彻的理解,也可以联系我,我在这里随时等着你. 对于css/html是每个 ...
- 如何使用正则做文本数据的清洗(附免费AI视频福利)
手工打造文本数据清洗工具 作者 白宁超 2019年4月30日09:43:59 前言:数据清理指删除.更正错误.不完整.格式有误或多余的数据.数据清理不仅仅更正错误,同样加强来自各个单独信息系统不同数据 ...
- docker常用命令(一)
1. docker命令 docker images //查看本地镜像 docker rmi 镜像名称:标签名称 //删除一个镜像 docker rm 容器ID //删除一个容器 docker comm ...
- 集合之保持compareTo和equals同步
在Java中我们常使用Comparable接口来实现排序,其中compareTo是实现该接口方法.我们知道compareTo返回0表示两个对象相等,返回正数表示大于,返回负数表示小于.同时我们也知道e ...