Solution
- 期望的题目真心不太会
- 定义状态(f[i])表示到第(i)期望长度,(dp[i])表示期望分数
- 如果上一步的持续(o)长度为(L),那么贡献是(L^2),现在长度为(L+1),贡献是(L^2+2*L+1),那么添加量就是(2*L+1)
- 所以我们可以得到转移方程:
(ch[i]==o) 时,(f[i]=f[i-1]+1 ~~~~~~~~~~~ dp[i]=dp[i-1]+f[i-1]*2+1)
(ch[i]==x) 时,(f[i]=0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ dp[i]=dp[i-1])
(ch[i]==?) 时,(f[i]=(f[i-1]+1)/2 ~~~~~dp[i]=dp[i-1]+(f[i-1]*2+1)/2)
Code
//it is coded by ning_mew on 7.22
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+7;
double ans=0,dp[maxn],f[maxn];
int n;
char ch[maxn];
int main(){
scanf("%d",&n); scanf("%s",ch);
for(int i=1;i<=n;i++){
if(ch[i-1]=='x'){f[i]=0;dp[i]=dp[i-1];continue;}
if(ch[i-1]=='o'){f[i]=f[i-1]+1;dp[i]=dp[i-1]+f[i-1]*2+1;continue;}
if(ch[i-1]=='?'){f[i]=0.5*f[i-1]+0.5;dp[i]=dp[i-1]+(f[i-1]*2+1)/2;continue;}
}printf("%0.4f
",dp[n]);return 0;
}
博主蒟蒻,随意转载。但必须附上原文链接:http://www.cnblogs.com/Ning-Mew/,否则你会场场比赛暴0!!!