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. centos7-vsftpd文件服务器

    FTP简介: 文件传输协议(File Transfer Protocol,FTP),基于该协议FTP客户端与服务端可以实现共享文件.上传文件.下载文件. FTP 基于TCP协议生成一个虚拟的连接,主要 ...

  2. WPF DataGridCheckBoxColumn需要点两次才能修改checkbox状态

    如题,如果必须要用DataGridCheckBoxColumn使用一下方式就可以解决需要点击两次才能改状态的问题 <DataGridCheckBoxColumn> <DataGrid ...

  3. c#中接口、抽象类、继承综合小练习

    namespace Test { class Program { static void Main(string[] args) { //作业:橡皮rubber鸭子.木wood鸭子.真实的鸭子real ...

  4. 关于UINavigationController的一些技巧

    未自定义任何东西的导航条效果如下: 1.自定义了 leftBarButtonItem 之后,左滑返回手势失效了,解决办法: self.navigationController.interactiveP ...

  5. 科技庄园(背包dp)---对于蒟蒻来说死了一大片的奇题

    题目描述: Life种了一块田,里面种了一些桃树. Life对PFT说:“我给你一定的时间去摘桃,你必须在规定的时间之内回到我面前,否则你摘的桃都要归我吃!” PFT思考了一会,最终答应了! 由于PF ...

  6. HashMap与ArrayMap(和SparseArray)的比较与选择

    HashMap与ArrayMap(和SparseArray)的比较与选择 2017年12月26日 06:04:38 阅读数:61 标签: androidjavahashmaparraymap数据结构 ...

  7. 点击tr实现选择checkbox功能,点击checkobx的时候阻止冒泡事件, jquery给checkbox添加checked属性或去掉checked属性不能使checkobx改变状态

    给tr添加点击事件,使用find方法查找tr下的所有层级的元素,children只查找下一层级的元素,所以使用find.find的返回值为jquery对象,在这个项目中不知道为什么使用jquery给c ...

  8. Verilog之语句位置

    1.if语句.case语句必须放在always过程语句块中. 2.verilog的系统函数比如:\(display/\)monitor必须放在initial 过程语句块中.这点尚为理解为何,但必须这样 ...

  9. Python3 S.join() 个人笔记

    S.join(iterable) S:需要的分隔符 iterable:被分割对象 . 注意括号里必须只能有一个成员,比如 ','.join('a','b') 这种写法是行不通的 实例:'-'.join ...

  10. leetcode-5-basic

    解题思路: 设两个变量land和sink,land的值是1的数量,sink表示内部的边.result = land*4-sink*2.按行扫描得到land, 同时得到同一行中内部边的数目:然后按列扫描 ...