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$ 转移到节点 $ ...
随机推荐
- Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
https://blog.csdn.net/hanchao5272/article/details/79819460 转载自:https://blog.bbzhh.com/index.php/arch ...
- mac 程序 Access-JSON-Data
mac 程序 Access-JSON-Data 说明 直接将JSON数据生成文件,便于调试. 效果图 源码 https://github.com/YouXianMing/Create-JSON-Mod ...
- 清除右键菜单CMD入口
批处理代码 reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\cmd" /v Extended /t REG_SZ
- VB ASP 使用 now() 时默认格式调整方法
修改注册表 [HKEY_USERS\.DEFAULT\Control Panel\International] "sShortDate"="yyyy-M-d" ...
- September 27th 2017 Week 39th Wednesday
We both look up at the same stars, yet we see such different things. 我们仰望同一片星空,却看见了不同的事物. Looking up ...
- Python Frame
http://farmdev.com/src/secrets/framehack/index.html sys._getframe([depth]) Return a frame object fro ...
- 字符串,元组,列表; 切片&range
总结:字符串: "" 组成元组: () 组成列表: [] 组成 切片 中括号冒号: [5: 0: -2] # print(s2[5:0:-2]) 此步长为-2,则从右往左取, 则a ...
- [李居丽][다이아몬드][Diamond]
歌词来源:http://music.163.com/#/song?id=484056974 作曲 : Damon Sharpe/JOHN HO/JEFF SHUM [作曲 : Damon Sharpe ...
- Hadoop学习之路(一)理论基础和逻辑思维
三个题目 第一题 问题描述 统计出当前这个一行一个IP的文件中,到底哪个IP出现的次数最多 解决思路 //必须要能读取这个内容 BufferedReader br = new BuffedReader ...
- spring-mybatis项目搭建(支持多数据源)
一.目录结构图 2.配置文件内容 db.properties: #oracle public oracle.driverClass=oracle.jdbc.driver.OracleDriver or ...