1444: [Jsoi2009]有趣的游戏

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1382  Solved: 498
[Submit][Status][Discuss]

Description

Input

注意 是0<=P

Output

Sample Input

Sample Output

HINT

 30%的数据保证, n ≤ 2. 50%的数据保证, n ≤ 5. 100%的数据保证, n , l, m≤ 10.

Source

析:很容易列出方程,dp[i] = ∑dp[j] * pj ,所以要处理出来就需要AC自动机,然后再用Gauss 消元即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 150000 + 10;
const int maxm = 3e5 + 10;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int maxnode = 10 * 10 + 50;
int sigma;
double A[maxnode][maxnode];
double p[15];
int pos[15]; struct Aho{
int ch[maxnode][11], f[maxnode];
bool val[maxnode];
int sz; void init(){ sz = 1; ms(ch[0], 0); }
inline int idx(char ch){ return ch - 'A'; } int insert(const char *s){
int u = 0;
for(int i = 0; s[i]; ++i){
int c = idx(s[i]);
if(!ch[u][c]){
ms(ch[sz], 0);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = 1;
return u;
} void getFail(){
queue<int> q; f[0] = 0;
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ q.push(u); f[u] = 0; }
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma; ++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];
}
}
} int solve(){
for(int i = 0; i < sz; ++i){
A[i][i] += 1.;
if(val[i]) continue;
for(int j = 0; j < sigma; ++j){
int nxt = ch[i][j];
A[nxt][i] -= p[j];
}
}
return sz;
}
};
Aho aho;
char s[20]; void Gauss(int n){
for(int i = 0; i < n; ++i){
int r = i;
for(int j = i+1; j < n; ++j)
if(fabs(A[j][i] > fabs(A[r][i]))) r = j;
if(r != i) for(int j = 0; j <= n; ++j) swap(A[r][j], A[i][j]); for(int k = i+1; k < n; ++k){
double f = A[k][i] / A[i][i];
for(int j = i; j <= n; ++j) A[k][j] -= f * A[i][j];
}
}
for(int i = n-1; i >= 0; --i){
for(int j = i+1; j < n; ++j)
A[i][n] -= A[j][n] * A[i][j];
A[i][n] /= A[i][i];
}
} int main(){
scanf("%d %d %d", &n, &m, &sigma);
for(int i = 0; i < sigma; ++i){
int x, y; scanf("%d %d", &x, &y);
p[i] = x * 1. / y;
}
aho.init();
for(int i = 1; i <= n; ++i){
scanf("%s", s);
pos[i] = aho.insert(s);
}
aho.getFail();
int len = aho.solve();
A[0][len] = 1.;
Gauss(len);
for(int i = 1; i <= n; ++i) printf("%.2f\n", A[pos[i]][len] / A[pos[i]][pos[i]]);
return 0;
}

  

BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)的更多相关文章

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

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

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

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

  3. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

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

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

  5. BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法

    题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...

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

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

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

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

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

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

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

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

随机推荐

  1. python 爬取网页基础 requests使用

    pip install requests  安装requests库 基本顺序: import requests r=requests.get("url路径") r.status_c ...

  2. 线特征---LineMatching原理(四)

    参考文章:An efficient and robust line segment matching approach based on LBD descriptor and pairwise geo ...

  3. YII2表单中上传单个文件

    有些时候我们提交的表单中含有文件.怎么样让表单里的数据和文件一起提交. 我的数据表tb_user内容如下: CREATE TABLE `tb_user` ( `id` int(11) unsigned ...

  4. C# 判断ip地址是否正确

    最后要用一方法判断ip地址是否正确,直接用.Net现成的类,方法如下: string ipStr="192.168.222.333"; IPAddress ip; if(IPAdd ...

  5. 标准IO缓冲机制

    参考资料: https://q16964777.iteye.com/blog/2228244 知道缓冲有几种模式:无缓冲.行缓冲.全缓冲.通过判断FILTE中的 _flags 的判断可以知道究竟是那种 ...

  6. RPDU

    RPDU(Remote Power Distribution Unit) 又称网络电源控制系统.远程电源管理系统.智能PDU.智能电源分配系统,是由傲视恒安科技(北京)有限公司自主研发生产并在全国范围 ...

  7. Writing modular applications with laravel-modules

    01-07-2016 Let me start by saying Laravel is an amazing framework. However when it comes to writing ...

  8. [Robot Framework] 怎么写动态等待?

    举例:Robot Framwork+WhiteLibrary,等待元素可用或不可用 Wait Until Object Is Enabled Wait Until Object Is Not Enab ...

  9. PHP--根据手机号-淘宝平台获取归属地运营商信息

    //获取手机账号信息 public function get_mobile_area($mobile){ $sms = array('province'=>'', 'supplier'=> ...

  10. Https如何确保传输安全的

    1.对称加密算法 加密和解密算法是公开的,那个密钥是保密的, 只有双方才知道, 这样生成的加密消息(密文) 别人就无法得知了. 2.非对称加密算法 RSA算法非常有意思,是有一对儿钥匙, 一个是保密的 ...