zoukankan      html  css  js  c++  java
  • UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率。再给出一些模板串S。从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串,

    问这个串不包含S中任何一个串的概率为多少?

    析:先构造一个AC自动机,然后随机生成L个字母,就是在AC自动机的某个结点走多少步,dp[i][j] 表示在 i 结点,并且剩下 j 步,

    然后记忆化搜索就OK了。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    const int maxnode = 20 * 20 + 10;
    const int sigma_size = 128;
    
    struct Aho_Corasick{
      int ch[maxnode][sigma_size];
      int f[maxnode];
      bool match[maxnode];
      int sz;
      void init(){ sz = 1;  memset(ch[0], 0, sizeof ch[0]); }
    
      void insert(char *s){
        int u = 0;
        for(int i = 0; s[i]; ++i){
          int c = s[i];
          if(!ch[u][c]){
            memset(ch[sz], 0, sizeof ch[sz]);
            match[sz] = 0;
            ch[u][c] = sz++;
          }
          u = ch[u][c];
        }
        match[u] = true;
      }
    
      int getFail(){
        queue<int> q;
        f[0] = 0;
        for(int c = 0; c < sigma_size; ++c){
          int u = ch[0][c];
          if(u){  f[u] = 0;   q.push(u);  }
        }
    
        while(!q.empty()){
          int r = q.front();  q.pop();
          for(int c = 0; c < sigma_size; ++c){
            int u = ch[r][c];
            if(!u){ ch[r][c] = ch[f[r]][c];  continue; }
            q.push(u);
            int v = f[r];
            while(v && !ch[v][c])  v = f[v];
            f[u] = ch[v][c];
            match[u] |= match[f[u]];
          }
        }
      }
    
    };
    Aho_Corasick ac;
    double dp[maxnode][110];
    bool vis[maxnode][110];
    char s[100];
    struct Node{
      int x;  double p;
    };
    vector<Node> v;
    
    double dfs(int u, int L){
      if(!L)  return 1.0;
      double &ans = dp[u][L];
      if(vis[u][L])  return ans;
      vis[u][L] = 1;
      ans = 0.0;
      for(int i = 0; i < n; ++i){
        int c = v[i].x;
        if(!ac.match[ac.ch[u][c]])  ans += dfs(ac.ch[u][c], L-1) * v[i].p;
      }
      return ans;
    }
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d", &n);
        ac.init();
        for(int i = 0; i < n; ++i){
          scanf("%s", s);
          ac.insert(s);
        }
        ac.getFail();
        scanf("%d", &n);
        v.clear();
        for(int i = 0; i < n; ++i){
          double pp;
          scanf("%s %lf", s, &pp);
          v.push_back((Node){s[0], pp});
        }
        int L;
        scanf("%d", &L);
        memset(vis, 0, sizeof vis);
        printf("Case #%d: %f
    ", kase, dfs(0, L));
      }
      return 0;
    }
    

      

  • 相关阅读:
    linux抓包命令tcpdump
    Linux ssh配置详解
    吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
    Python装饰器详解
    centos7 安装redis
    C# 操作Exchange 的powershell以实现邮件撤回
    C# 委托的理解和案例
    IIS10 http重定向https
    程序员修炼之道 | 不要让你的代码走上渡渡鸟的灭绝之路
    离子烫攻略
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6740583.html
Copyright © 2011-2022 走看看