zoukankan      html  css  js  c++  java
  • 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
    ");
     99     else printf("%.6lf
    ",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 }
  • 相关阅读:
    azkaban使用--指定executor
    azkaban使用--依赖dependencies作业
    azkaban用户管理及权限配置
    azkaban架构介绍
    azkaban工作流调度器及相关工具对比
    azkaban的简单使用
    Azkaban安装及分布式部署(multiple-executor)
    配置yum源
    idea搭建Eureka注册中心及配置密码登陆
    kudu导入文件(基于impala)
  • 原文地址:https://www.cnblogs.com/xiong-/p/3915538.html
Copyright © 2011-2022 走看看