[SDOI2017]硬币游戏 Hash+高斯消元

Description
给你一个字符串集,构造一个串每个位置等概率的插入。
问字符串集中每个字符串最先出现在构造的串中的概率。


Sample Input
3 3
THT
TTH
HTT


Sample Output
0.3333333333
0.2500000000
0.4166666667


首先这道题有弱化版,就是 J S O I 2009 奇 怪 的 游 戏 JSOI2009奇怪的游戏 JSOI2009
但是直接暴力高消不是很行。
考虑更为巧妙地优化状态。
我们设一个未匹配任何串的状态为 S S S,它的概率为 p s p_s ps
一个串 i i i匹配成功的概率为 p i p_i pi
假设有一个串 A = H T T , B = T T H A=HTT,B=TTH A=HTT,B=TTH
但是 S S S不一定是加入了 T T H TTH TTH之后才满足了匹配。他有可能有
S B SB SB S ′ + A + H S'+A+H S+A+H或者 S ′ ′ + A + T H S''+A+TH S+A+TH这种形式,也就是说可以列出这种方程:
1 8 p s = p 1 + 3 4 p 2 \frac 18p_s=p_1+\frac 34p_2 81ps=p1+43p2
那么只用暴力判一个串的前缀是不是另一个串的后缀,然后列方程即可。
方程数为 n + 1 n+1 n+1个,那么就直接爆解方程组即可。


#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int _max(int x, int y) {return x > y ? x : y;}
int _min(int x, int y) {return x < y ? x : y;}
const int N = 302;
const ULL P = 131;
int read() {
    int s = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * f;
}
void put(int x) {
    if(x >= 10) put(x / 10);
    putchar(x % 10 + '0');
}

char ss[N];
ULL s[N][N], o[N];
double po[N], f[N][N];

void gauss(int n) {
    for(int i = 1; i <= n; i++) {
        int now = i;
        for(int j = i; j <= n; j++) if(fabs(f[j][i]) > fabs(f[now][i])) now = j;
        swap(f[now], f[i]);
        for(int j = i + 1; j <= n; j++) {
            double hh = f[j][i] / f[i][i];
            for(int k = i; k <= n + 1; k++) f[j][k] -= hh * f[i][k];
        }
    } for(int i = n; i >= 1; i--) {
        for(int j = i - 1; j >= 1; j--) {
            double hh = f[j][i] / f[i][i];
            f[j][i] = 0, f[j][n + 1] -= hh * f[i][n + 1];
        }
    }
}

int main() {
    int n = read(), m = read();
    po[0] = 1.0; for(int i = 1; i <= m; i++) po[i] = po[i - 1] * 0.5;
    o[0] = 1; for(int i = 1; i <= m; i++) o[i] = o[i - 1] * P;
    for(int i = 1; i <= n; i++) {
        scanf("%s", ss + 1);
        for(int j = 1; j <= m; j++) s[i][j] = s[i][j - 1] * P + ss[j];
    } for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            for(int k = 1; k <= m; k++) {
                if(s[i][k] == s[j][m] - s[j][m - k] * o[k]) {
                    f[i][j] += po[m - k];
                }
            }
        }
    } for(int i = 1; i <= n; i++) f[i][n + 1] -= po[m], f[n + 1][i] = 1.0; f[n + 1][n + 2] = 1.0;
    gauss(n + 1);
    for(int i = 1; i <= n; i++) {
        double gg = f[i][n + 2] / f[i][i];
        printf("%.8lf\n", gg);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值