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

1444: [Jsoi2009]有趣的游戏

题意:每种字母出现概率\(p_i\),有一些长度len的字符串,求他们出现的概率


套路DP的话,\(f[i][j]\) i个字符走到节点j的概率,建出转移矩阵来矩乘几十次可以认为是无穷个字符,就得到概率了

但我们发现Trie图也是图啊,直接高斯消元就好了,\(f[i]\)表示走到节点i的期望次数

注意\(f[0]\)需要+1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=105;
const double eps=1e-8;
inline int read(){char c=getchar();int x=0,f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return x*f;
}int n, len, m, pos[N]; double p[N], x, y;
char s[N];
namespace ac{struct meow{int ch[11], fail, val;} t[N];int sz;void insert(char *s, int id) {int u=0;for(int i=1; i<=len; i++) {int c=s[i]-'A';if(!t[u].ch[c]) t[u].ch[c] = ++sz;u=t[u].ch[c]; }t[u].val=1; pos[id]=u;}int q[N], head, tail;void build() {head=tail=1;for(int i=0; i<m; i++) if(t[0].ch[i]) q[tail++] = t[0].ch[i];while(head!=tail) {int u=q[head++];t[u].val |= t[t[u].fail].val;for(int i=0; i<m; i++) {int &v = t[u].ch[i];if(!v) v = t[t[u].fail].ch[i];else t[v].fail = t[t[u].fail].ch[i], q[tail++]=v;}}}
}using ac::t; using ac::sz;double a[N][N];
namespace eq{void build() { a[0][sz+1] = 1;for(int i=0; i<=sz; i++) {  //printf("i %d\n",i);a[i][i]=1;if(!t[i].val) for(int j=0; j<m; j++) a[ t[i].ch[j] ][i] -= p[j];// printf("ch %d %lf  %d\n",j,p[j],t[i].ch[j]);}//for(int i=0; i<=n; i++) for(int j=0; j<=n+1; j++) printf("%lf%c",a[i][j],j==n+1?'\n':' ');}void gauss(int n) {for(int i=0; i<=n; i++) {int r=i;for(int j=i; j<=n; j++) if(abs(a[j][i])>abs(a[r][i])) r=j;if(r!=i) for(int j=0; j<=n+1; j++) swap(a[r][j], a[i][j]);for(int k=i+1; k<=n; k++) {double t = a[k][i]/a[i][i];for(int j=i; j<=n+1; j++) a[k][j] -= t*a[i][j];}}for(int i=n; i>=0; i--) {for(int j=n; j>i; j--) a[i][n+1] -= a[i][j]*a[j][n+1];a[i][n+1] /= a[i][i];}}
}
int main() {freopen("in","r",stdin);n=read(); len=read(); m=read(); int flag=0;for(int i=0; i<m; i++) x=read(), y=read(), p[i]=(double)x/y, flag |= p[i]>eps;if(!flag) {for(int i=1; i<=n; i++) puts("0.00"); return 0;}for(int i=1; i<=n; i++) scanf("%s",s+1), ac::insert(s, i);ac::build();eq::build(); eq::gauss(sz);//for(int i=1; i<=n; i++) printf("%d ",pos[i]); puts(" pos");for(int i=1; i<=n; i++) printf("%.2lf\n", a[pos[i]][sz+1]);
}

转载于:https://www.cnblogs.com/candy99/p/6666722.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/401235.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Oracle安装部署之RedHat安装Oracle11g_R2

硬件配置 内存 &#xff1a;≥1G 硬盘空间&#xff1a;≥10G 上传oracle11g安装包&#xff1a; putty上用wcw用户登录&#xff0c;通过ftp服务上传oracle安装文件到/home/wcw目录下解压 #unzip linux_11gR2_database_1of2.zip #unzip linux_11gR2_database_2of2.zip 检查和安装…

Fans没信心,回家继续修行

今天在CSDN上看了一篇的文章&#xff0c;感觉自己实在是太菜了&#xff0c;以至于对毕业之后从事IT行业没有了任何信心。现在也不清楚&#xff0c;自己能否在it行业混下去。自己的技术实在是一个水啊。8号就要回家了&#xff0c;兄弟姐妹们如果有事情&#xff0c;请发短信至 15…

查找字符串中要查找的字符串最后一次出现的位置

C Code 123456789101112131415161718192021222324#include <stdio.h>#include <string.h>//查找字符串中要查找的字符串最后一次出现的位置 char *strrstr (const char*string, const char*str){char *index NULL;char *ret NULL;int i 0;do{index strstr(stri…

基于SqlSugar的数据库访问处理的封装,支持多数据库并使之适应于实际业务开发中

Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/course/detail/35475 在我的各种开发框架中&#xff0c;数据访问有的基于微软企业库&#xff0c;有的基于EFCore的实体框架&#xff0c;两者各有其…

Unity 实现物体破碎效果(转)

感谢网友分享&#xff0c;原文地址&#xff08;How to Make an Object Shatter Into Smaller Fragments in Unity&#xff09;&#xff0c;中文翻译地址&#xff08;Unity实现物体破碎效果&#xff09; In this tutorial I will show you how to create a simple shattering ef…

【转】/usr/bin/python^M: bad interpreter: No such file

转自&#xff1a;http://hanbaobao2005.blog.51cto.com/647054/635256今天在WingIDE下写了个脚本&#xff0c;传到服务器执行后提示&#xff1a; -bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No such file or directory 分析&#xff1a; 这是不同系统编…

PyTorch常用参数初始化方法详解

Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/course/detail/35475 1、均匀分布初始化 torch.nn.init.uniform_(tensor, a0, b1) 从均匀分布U(a, b)中采样&#xff0c;初始化张量。  参数…

sql语句中的删除操作

drop: drop table tb; 删除内容和定义&#xff0c;释放空间。简单来说就是把整个表去掉。以后不能再新增数据&#xff0c;除非新增一个表。 truncate&#xff1a; truncate table tb; 删除内容、释放空间但不删除定义&#xff0c;即只是清空数&#xff0c;不会删除表的数据结构…

[面试题]事件循环经典面试题解析

Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/course/detail/35475 基础概念 进程是计算机已经运行的程序,线程是操作系统能够进行运算调度的最小单位,它被包含在进程中.浏览器中每开一个Tab…

CC254x--OSAL

OSAL运行原理 蓝牙协议栈PROFILE、所有的应用程序、驱动等都是围绕着OSAL组织运行的。OSAL&#xff08;Operating System Abstraction Layer&#xff09;操作系统抽象层&#xff0c;它不是一个真正的操作系统&#xff08;它没有 Context Switch 上下文切换功能&#xff09;&am…

CLR 与 C++的常用类型转换笔记

1. System::String 转换到 const wchar_t* const wchar_t* ToUnmanagedUnicode( System::String^ str ){ pin_ptr<const WCHAR> nativeString1 PtrToStringChars( str ); return (const wchar_t*)nativeString1;} 2. const wchar_t* / const char* 转换到 System::Strin…

mysql跨节点join——federated引擎

一、 什么是federated引擎 mysql中的federated类似于oracle中的dblink。 federated是一个专门针对远程数据库的实现&#xff0c;一般情况下在本地数据库中建表会在数据库目录中生成相对应的表定义文件&#xff0c;并同时生成相对应的数据文件。 [图] 但是通过federated引擎创建…

【阅读SpringMVC源码】手把手带你debug验证SpringMVC执行流程

Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/course/detail/35475 ✿ 阅读源码思路&#xff1a; 先跳过非重点&#xff0c;深入每个方法&#xff0c;进入的时候可以把整个可以理一下方法的执…

Zabbix监控(十六):分布式监控-Zabbix Proxy

说明&#xff1a;Zabbix支持分布式监控&#xff0c;利用Proxy代理功能&#xff0c;在其他网络环境中部署代理服务器&#xff0c;将监控数据汇总到Zabbix主服务器&#xff0c;实现多网络的分布式监控&#xff0c;集中监控。1、分布式监控原理Zabbix proxy和Zabbix server一样&am…

CC254x--BLE

BLE协议栈 BLE体系结构&#xff0c;着重了解GAP和GATT。 PHY物理层在2.4GHz的ISM频段中跳频识别。LL连接层&#xff1a;控制设备的状态。设备可能有5中状态&#xff1a;就绪standby&#xff0c;广播advertising&#xff0c;搜索scanning&#xff0c;初始化initiating和连接con…

DW 在onload运行recordset find.html时 发生了以下javascript错误

这两天打开Dreamweaver CS5&#xff0c;总是弹出一个错误&#xff0c;写着&#xff1a; 在onLoad运行RecordsetFind.htm时&#xff0c;发生了以下JavaScript错误&#xff1a; 在文件“RecordsetFind”中&#xff1a; findRsisnotdefined 在关闭Dreamweaver的时候也会弹出一个类…

Azure Container App(一)应用介绍

Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/course/detail/35475 一&#xff0c;引言 容器技术正日益成为打包、部署应用程序的第一选择。Azure 提供了许多使用容器的选项。例如&#xff0…

CC254x--API

CC2541常用API 连接 定义广播数据 GAPRole_SetParameter(GAPROLE_ADVERT_DATA,…); 自定义扫描响应数据 GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA,…); 密码管理回调 ProcessPasscodeCB(); 状态管理回调 peripheralStateNotificationCB(); 通信控制 添加GATT服务 GATTServ…

mysql 分区信息查看

select partition_name part,partition_expression expr,partition_description descr,table_rows from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMASCHEMA() AND TABLE_NAMEmx_domain//查看分区信息 CREATE TABLE mx_domain (id int(10) NOT NULL AUTO_INCREMENT,name…