Crazy Typewriter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 391    Accepted Submission(s): 109

Problem Description
There was a crazy typewriter before. When the writer is not very sober, it would type characters randomly, one per second, the possiblities of characters may differ.
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

 
Input
There are several cases, no more than 15.
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'.
 
Output
A single line contains the expectation of time for each case, in seconds with six decimal, if the exception doesn't exist, output "Infinity"
 
Sample Input
2
0.5000 0.5000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
ab
aa
 
Sample Output
3.000000
 
 #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自动机上的高斯消元求期望的更多相关文章

  1. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  2. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  3. 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 ...

  4. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...

  5. hdu 2262 高斯消元求期望

    Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  6. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  8. 高斯消元与期望DP

    高斯消元可以解决一系列DP序混乱的无向图上(期望)DP DP序 DP序是一道DP的所有状态的一个排列,使状态x所需的所有前置状态都位于状态x前: (通俗的说,在一个状态转移方程中‘=’左侧的状态应该在 ...

  9. 【BZOJ2137】submultiple 高斯消元求伯努利数

    [BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...

随机推荐

  1. linux 安装并且设置环境lua环境变量

    在lua官网下载lua安装包并安装: http://www.lua.org/download.html 解压编译: wget http://www.lua.org/ftp/lua-5.3.2.tar. ...

  2. iOS开发之WIFI,3G/4G两种网络同时使用技巧

    最近遇到一个比较奇葩的需求:App与硬件通过WiFi LAN通信, 同时App需要与服务器通过3G/4G WAN通信,如下图: 众所周知,手机同时打开WiFi和3G时候,会优先走WiFi.这个该如何实 ...

  3. UIViewController 的 edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets属性

    1.有时你命名设置了某控件的y坐标为0,确总是被导航栏遮挡住,如下: UILabel *label = [[UILabel alloc] init];    label.text = @"请 ...

  4. [LUOGU] 4149 [IOI2011]Race

    点分治裸题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; i ...

  5. composer安装laravel-u-editor及其使用

    前言  使用的框架是laravel5.1,是composer搭建的,可以直接配置composer,如果不是composer搭建的larave,需要先安装composer,具体安装发放可以参考compo ...

  6. 【Python学习之五】高级特性5(切片、迭代、列表生成器、生成器、迭代器)

    5.迭代器 由之前的生成器可知,for循环用于可迭代对象:Iterable.包括集合数据类型: list.tuple.dict.set.str 等,以及两种生成器.判断迭代器,使用 isinstanc ...

  7. 如何下载并安装 robomongo 到Ubuntu 系统

    官网下载软件,https://robomongo.org/download wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-l ...

  8. Spring Cloud构建微服务架构(三)消息总线

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  9. 使用nohup+& 踩到的坑

    首先分清楚nohup与&: &是指在后台运行一般在执行命令后,都会显式的在前台执行,当Ctrl+C后进程回宕掉,但是 在命令后加&,即使Ctrl+C,程序还在进行,但是,当关闭 ...

  10. UVa - 12096 集合栈计算机(STL)

    [题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两 ...