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

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

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <algorithm>
  6 #include <cmath>
  7 using namespace std;
  8
  9 const int N=250;
 10 const int SZ=26;
 11 const double eps=1e-8;
 12 double p[27],A[N][N];
 13 bool inf[N];
 14 struct AC
 15 {
 16     int ch[N][26];
 17     int sz,val[N],fail[N];
 18     void init(){
 19         sz=1;memset(val,0,sizeof(val));
 20         memset(ch[0],0,sizeof(ch[0]));
 21     }
 22     void insert(char *s)
 23     {
 24         int rt=0;
 25         for (int i=0;s[i];i++){
 26             int c=s[i]-‘a‘;
 27             if (ch[rt][c]==0){
 28                 ch[rt][c]=sz;
 29                 memset(ch[sz],0,sizeof(ch[sz]));
 30                 sz++;
 31             }
 32             rt=ch[rt][c];
 33         }
 34         val[rt]=1;
 35     }
 36     void getfail()
 37     {
 38         queue<int>q;
 39         for (int i=0;i<SZ;i++)
 40         {
 41             int c=ch[0][i];
 42             if (c){ q.push(c);fail[c]=0; }
 43         }
 44         while (!q.empty())
 45         {
 46             int u=q.front();q.pop();
 47             for (int i=0;i<SZ;i++)
 48             {
 49                 int c=ch[u][i];
 50                 if (!c){
 51                     ch[u][i]=ch[fail[u]][i];
 52                 }else
 53                 {
 54                     int v=fail[u];
 55                     q.push(c);
 56                     fail[c]=ch[v][i];
 57                     val[c]|=val[fail[c]];
 58                 }
 59             }
 60         }
 61     }
 62 }ac;
 63
 64 void build_matrix()
 65 {
 66     int i,j;
 67     memset(A,0,sizeof(A));
 68     for(i=0;i<ac.sz;i++)
 69     {
 70         if(ac.val[i]){A[i][i]=1;A[i][ac.sz]=0;}
 71         else
 72         {
 73             for(j=0;j<SZ;j++){int v=ac.ch[i][j];A[i][v]+=p[j];}
 74             A[i][i]+=-1;A[i][ac.sz]=-1;
 75         }
 76     }
 77 }
 78
 79 void gauss(int n)
 80 {
 81     int i,j,k,r;
 82     for(i=0;i<n;i++)
 83     {
 84         r=i;
 85         for(j=i+1;j<n;j++)
 86             if(fabs(A[j][i])>fabs(A[r][i])) r=j;
 87             if(fabs(A[r][i])<eps) continue;
 88             if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]);
 89             for(k=0;k<n;k++) if(k!=i)
 90                 for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
 91     }
 92     memset(inf,0,sizeof(inf));
 93     for(i=ac.sz-1;i>=0;i--){
 94         if(fabs(A[i][i])<eps&&fabs(A[i][ac.sz])>eps) inf[i]=1;
 95         for(j=i+1;j<ac.sz;j++)
 96             if(fabs(A[i][j])>eps&&inf[j]) inf[i]=1;
 97     }
 98     if(inf[0]) printf("Infinity\n");
 99     else printf("%.6lf\n",A[0][ac.sz]/A[0][0]+eps);
100 }
101
102 int main()
103 {
104     //freopen("b.txt","w",stdout);
105     int n,i,j;char s[20];
106     while(~scanf("%d",&n))
107     {
108         ac.init();
109         for(i=0;i<SZ;i++) scanf("%lf",p+i);
110         for(i=0;i<n;i++)
111         {
112             scanf("%s",s);ac.insert(s);
113         }
114         ac.getfail();
115         build_matrix();
116         gauss(ac.sz);
117     }
118     return 0;
119 }

hdu 3992 AC自动机上的高斯消元求期望,布布扣,bubuko.com

时间: 2024-10-14 04:59:42

hdu 3992 AC自动机上的高斯消元求期望的相关文章

[ACM] hdu 2262 Where is the canteen (高斯消元求期望)

Where is the canteen Problem Description After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is t

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

hdu 4418 高斯消元求期望

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1480    Accepted Submission(s): 327 Problem Description Agent K is one of the greatest agents in a secret organization called Men in B

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

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 415 Special Judge Problem Description A little gir

hdu 4870 Rating(高斯消元求期望)

http://acm.hdu.edu.cn/showproblem.php?pid=4870 题意:有两个号,初始分数都是0,每次选一个分数较小的打比赛,如果分数一样任选一个,有p的概率涨50分,最高为1000分,有1-p的概率跌100分,最低为0分.问有一个号涨到1000需要打比赛的次数的期望. 令(x, y)表示高分为x,低分为y的状态(x >= y),E(x, y)表示从(x, y)到达(1000, ?)的比赛场数期望.容易得到E(x, y) = P * E(x1, y1) + (1 - 

hdu 1071 The area 高斯消元求二次函数+辛普森积分

构造系数矩阵,高斯消元求解二次函数,然后两点式求直线函数,带入辛普森积分法无脑AC... #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<vector> #include<cmath> using namespace std; struct node { double x,y; }p[4]; double g[10][10]

高斯消元 求整数解模版

#include <iostream> #include <string.h> #include <cmath> using namespace std; const int maxn = 105; int equ, var; // 有equ个方程,var个变元.增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var. int a[maxn][maxn]; int x[maxn]; // 解集. bool free_x[maxn]; //

uva 10828 高斯消元求数学期望

Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name of Kernighan and Ritchie, the authors of The C Programming Language. While coding in C, we use different control statements and loops, such as, if-the

【bzoj2460】[BeiJing2011]元素 贪心+高斯消元求线性基

题目描述 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石.一般地,矿石越多则法力越强,但物极必反:有时,人们为了获取更强的法力而使用了很多矿石,却在炼制过程中发现魔法矿石全部消失了,从而无法炼制出法杖,这个现象被称为“魔法抵消” .特别地,如果在炼制过程中使用超过一块同一种矿石,那么一定会发生“魔法抵消”. 后来,随着人们认知水平的提高,这个现象得到了很好的解释.经过了大量的实验后,著名法师 D