[Codeforces 204C] Little Elephant and Furik and Rubik (期望)

Codeforces - 204C

给定两个长度相等字符串,每次随机各从中选两个长度相等的子串
问两个子串对应位置相等个数的期望值


枚举 A中的每个字符,然后算贡献值
预处理一下 B中相等字符的前缀和即可
总的复杂度是 (N)
注意分子可能会爆LL,所以开成double的

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("\n----------")

const int maxn=2e5+10;
int N;
char A[maxn], B[maxn];
LL Cnt[26];

DBL calc();

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    while(~scanf("%d", &N))
    {
        scanf(" %s %s", A+1, B+1);
        DBL ans = calc();
        reverse(A+1, A+1+N);
        reverse(B+1, B+1+N);
        ans += calc();
        for(int i=1; i<=N; i++) if(A[i]==B[i])
        {
            ans += (LL)i*(N-i+1);
        }
        DBL tot = (DBL)N*(N+1)*(2*N+1)/6;
        printf("%.9f\n", ans/tot);
    }
    return 0;
}

DBL calc()
{
    CLR(Cnt);
    DBL res=0;
    for(int i=1; i<=N; i++)
    {
        res += Cnt[A[i]-'A']*(N-i+1);
        Cnt[B[i]-'A'] += i;
    }
//  printf("%lld\n", res);
    return res;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值