hdu 3992 AC自动机上的高斯消元求期望
Crazy Typewriter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 391 Accepted Submission(s): 109
The protagonist in this problem wants to tell acmers some secrets, of course, by the typewriter.
There had been several opportunities, but the protagonist let them sliped. Now, another opportunity came, the writer started a new paragraph. The protagonist found
that he could set the possiblities of each character in happy astonishment. After the possiblities had been set, he wanted to know the exception of time at least the writer need to be mind-absent if any secret was typed out.
fewovigwnierfbiwfioeifaorfwarobahbgssjqmdowj
The first line of each case contains an integer n, no more than 15, indicating the number of secrets.
The second line contains 26 real numbers, indicating the set possibilities of 'a'-'z', respectively, the sum would be 1.0 .
Then n lines, each contains a secret, no longer than 15, which is made up by lowercase letters 'a'-'z'.
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std; const int N=;
const int SZ=;
const double eps=1e-;
double p[],A[N][N];
bool inf[N];
struct AC
{
int ch[N][];
int sz,val[N],fail[N];
void init(){
sz=;memset(val,,sizeof(val));
memset(ch[],,sizeof(ch[]));
}
void insert(char *s)
{
int rt=;
for (int i=;s[i];i++){
int c=s[i]-'a';
if (ch[rt][c]==){
ch[rt][c]=sz;
memset(ch[sz],,sizeof(ch[sz]));
sz++;
}
rt=ch[rt][c];
}
val[rt]=;
}
void getfail()
{
queue<int>q;
for (int i=;i<SZ;i++)
{
int c=ch[][i];
if (c){ q.push(c);fail[c]=; }
}
while (!q.empty())
{
int u=q.front();q.pop();
for (int i=;i<SZ;i++)
{
int c=ch[u][i];
if (!c){
ch[u][i]=ch[fail[u]][i];
}else
{
int v=fail[u];
q.push(c);
fail[c]=ch[v][i];
val[c]|=val[fail[c]];
}
}
}
}
}ac; void build_matrix()
{
int i,j;
memset(A,,sizeof(A));
for(i=;i<ac.sz;i++)
{
if(ac.val[i]){A[i][i]=;A[i][ac.sz]=;}
else
{
for(j=;j<SZ;j++){int v=ac.ch[i][j];A[i][v]+=p[j];}
A[i][i]+=-;A[i][ac.sz]=-;
}
}
} void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(fabs(A[r][i])<eps) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
memset(inf,,sizeof(inf));
for(i=ac.sz-;i>=;i--){
if(fabs(A[i][i])<eps&&fabs(A[i][ac.sz])>eps) inf[i]=;
for(j=i+;j<ac.sz;j++)
if(fabs(A[i][j])>eps&&inf[j]) inf[i]=;
}
if(inf[]) printf("Infinity\n");
else printf("%.6lf\n",A[][ac.sz]/A[][]+eps);
} int main()
{
//freopen("b.txt","w",stdout);
int n,i,j;char s[];
while(~scanf("%d",&n))
{
ac.init();
for(i=;i<SZ;i++) scanf("%lf",p+i);
for(i=;i<n;i++)
{
scanf("%s",s);ac.insert(s);
}
ac.getfail();
build_matrix();
gauss(ac.sz);
}
return ;
}
hdu 3992 AC自动机上的高斯消元求期望的更多相关文章
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元
http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...
- [ACM] hdu 4418 Time travel (高斯消元求期望)
Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...
- hdu 2262 高斯消元求期望
Where is the canteen Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu 4418 高斯消元求期望
Time travel Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- 高斯消元与期望DP
高斯消元可以解决一系列DP序混乱的无向图上(期望)DP DP序 DP序是一道DP的所有状态的一个排列,使状态x所需的所有前置状态都位于状态x前: (通俗的说,在一个状态转移方程中‘=’左侧的状态应该在 ...
- 【BZOJ2137】submultiple 高斯消元求伯努利数
[BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...
随机推荐
- 《队长说得队》【Alpha】Scrum meeting 1
项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 ...
- 将unity3d项目嵌入到Android App中使用
创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...
- Android深度探索总结
Android深度探索前四章总结 通过这几章的学习真实体会到“移植”的概念:为特定设备定制Android的过程,但是移植的过程中开发最多的就是支持各种硬件设备的Linux驱动程序,本章对Android ...
- Git - revert详解
git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销作为一次最新的提交 * git revert HEAD ...
- (69)zabbix监控惠普打印机
假设公司有多个楼层或者分布在不同楼,打印机自然分布很广泛,打印机缺少油墨或者卡纸了,都需要员工找IT部门.我们使用zabbix对打印机进行监控,一旦缺少油墨,zabbix发出报警,it人员能够及时更换 ...
- 笔记--Day1--python基础1
一.目录 1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum),目前已经是使用频度特别高的开发语言. 主要应用领域: 云计算:云计算最火的语言,典型应用有Op ...
- python入门:if和else的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #2.X用raw_input,3.X用input #if和else的基本用法 name = input(&qu ...
- Python头脑风暴4
IT是全国平均薪资最高的行业,2017年全国最高,人均13点4万每年. 但技术固然好,创业拼的还是世界观下的创意. 蘑菇街,并夕夕,TikTok,头条,哪个不是创意用IT技术的现实化?? 未来,大平台 ...
- debian软raid
http://www.linuxidc.com/Linux/2013-06/86487.htm
- <原创>在PE最后一节中插入补丁程序(附代码)
完整文件 http://files.cnblogs.com/Files/Gotogoo/在PE最后一节中插入补丁程序.zip 在PE文件最后一节中插入补丁程序,是最简单也是最有效的一种,因为PE最后 ...