zoukankan      html  css  js  c++  java
  • UVaLive 3490 Generator (KMP + DP + Gauss)

    题意:随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度。

    析:由于只要包含目标串就可以停止,所以可以先把这个串进行处理,也就是KMP,然后dp[i] 表示从 i 结点到完全匹配期望长度,所以很容易得到状态转移方程 dp[i] = ∑dp[j] / n + 1,然后用高斯消元即可,要注意,要用全整数的高斯消元。

    代码如下:

    #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>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #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 fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    //#define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #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 = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 150000 + 10;
    const int maxm = 3e5 + 10;
    const int mod = 10007;
    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;
    }
    
    char s[20];
    int f[maxn];
    LL A[20][20];
    
    
    void getFail(int n){
      f[0] = f[1] = 0;
      for(int i = 1; i < n; ++i){
        int j = f[i];
        while(j && s[i] != s[j])  j = f[j];
        f[i+1] = s[i] == s[j] ? j+1 : 0;
      }
    }
    
    void Gauess(int n){
      for(int i = 0; i < n; ++i){
        int r = i;
        while(r < n && !A[r][i])  ++r;
        if(r != i)  for(int j = 0; j <= n; ++j)  swap(A[r][j], A[i][j]);
    
        for(int k = i+1; k < n; ++k) if(A[k][i]){
          LL f = A[k][i];
          for(int j = i; j <= n; ++j)  A[k][j] = A[k][j] * A[i][i] - f * A[i][j];
        }
      }
      for(int i = n-1; i >= 0; --i){
        for(int j = i+1; j < n; ++j)
          A[i][n] -= A[j][n] * A[i][j];
        A[i][n] /= A[i][i];
      }
    }
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d %s", &n, s);
        m = strlen(s);
        getFail(m);
        ms(A, 0);
        for(int i = 0; i < m; ++i){
          A[i][i] += n;
          A[i][m+1] += n;
          for(int k = 0; k < n; ++k){
            int j = i;
            while(j && s[j] != 'A' + k)  j = f[j];
            if(s[j] == 'A' + k)  ++j;
            --A[i][j];
          }
        }
        A[m][m] = 1;
        Gauess(m + 1);
        printf("Case %d:
    ", kase);
        printf("%lld
    ", A[0][m+1]);
        if(kase != T)  puts("");
      }
      return 0;
    }
    

      

  • 相关阅读:
    PHP延迟静态绑定
    PHP SPL神器实现堆排序
    魔术方法__sleep 和 __wakeup
    SPL 笔记
    PHP中对象的深拷贝与浅拷贝
    PHP的垃圾回收机制详解
    深入理解PHP中赋值与引用
    xdebug安装及使用小结
    工作中碰到的一个问题(cookie相关)
    分享一个解析XML成为php数组的方法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7846382.html
Copyright © 2011-2022 走看看