JZOJ 2934. 【NOIP2012模拟8.7】字符串函数
题目大意
个等长的由大写英文字母构成的字符串 \(a\) 和 \(b\),从 \(a\) 中选择连续子串 \(x\),从 \(b\) 中选出连续子串y。
定义函数 \(f_{x,y}\) 为满足条件 \(x_i=y_i(1<=i<=|x|)\) 的 \(i\) 的个数,计算 \(f_{x,y}\) 的数学期望。\((|x|=|y|)\)
分析
其实这题没什么
如果你读懂了题
只需要考虑每一位 \(i\) 的贡献即可
\(Code\)
#include<cstdio>
#include<string>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int n , f[N][26] , g[N][26];
LL ans , tot;
char s1[N] , s2[N];
int main()
{
scanf("%d%s%s" , &n , s1 + 1 , s2 + 1);
for(register int i = 1; i <= n; i++)
{
for(register int j = 0; j < 26; j++) f[i][j] = f[i - 1][j];
f[i][s1[i] - 'A'] += i;
}
for(register int i = n; i; i--)
{
for(register int j = 0; j < 26; j++) g[i][j] = g[i + 1][j];
g[i][s1[i] - 'A'] += n - i + 1;
}
for(register int i = 1; i <= n; i++)
{
ans += 1LL * f[i][s2[i] - 'A'] * (n - i + 1) + 1LL * g[i + 1][s2[i] - 'A'] * i;
tot += 1LL * i * i;
}
printf("%lf" , 1.0 * ans / tot);
}