题目大意:有一个字符串,由 ’ o ‘, ’ x ‘, ’ ? ’ 组成,连续的 Y 个 ’ o ’ 对答案的贡献是 Y ^ 2,而 ’ x ’ 不会产生贡献,’ ? ’ 代表有 50% 的几率代表 ’ o ‘,另一半代表 ’ x ‘,求期望的贡献
题解:记L为当前期望后缀o长度,f[i]表示以第i个字符结尾的期望得分
(1)s[i]=x,L=0,f[i]=f[i−1]
(1)s[i]=o,L=L+1,得分从L2变为(L+1)2,对答案贡献2L+1,f[i]=f[i−1]+2L+1
(1)s[i]=?,由于是等概率,自然是上面两种情况取平均,L=L+12,f[i]=f[i−1]+2L+12
我的收获:对题目的分析……
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int n;
double f[300005],L;
char s[300005];
void init()
{
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;i++){
switch(s[i])
{
case 'x':f[i]=f[i-1],L=0;break;
case 'o':f[i]=f[i-1]+2*L+1,L=L+1;break;
default :f[i]=f[i-1]+(2*L+1)/2,L=(L+1)/2;
}
}
printf("%.4lf\n",f[n]);
}
int main()
{
init();
return 0;
}