题目链接:http://codeforces.com/problemset/problem/509/E

题意:给你一个字符串,求字串中包括子串中I, E, A, O, U, Y.所占的概率和。

题解:有些技巧的题目,关于概率之和可以考虑每个点单独处理然后最终求和。

假设i点是I, E, A, O, U, Y中的一个。

以i为终点的概率之和。(字符串从1开始)

1+1/2+1/3+....1/i=sum[i];

以i为起点不包括i单点的情况。

1/2+1/3+....1/(len-i+1)=sum[len-i+1]-sum[1];

以i为中间点分类讨论。

字符串中i的位置为2时。

1/3+1/4+1/5+....1/(len-i+2)=sum[len-i+2]-sum[2];

位置为3时

1/3+1/4+1/5+....1/(len-i+3)=sum[len-i+3]-sum[3];

....

位置为i时

1/(i+1)+1/(i+2)+....1/len=sum[len]-sum[i];

最后全部加起来

sum[len]+sum[len-1]+...sum[len-i+1]-(sum[i]+s[i-1]+....sum[1])=tot[len]-tot[len-i]-tot[i];

(tot表示前缀和的前缀和)

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int M = 5e5 + 10;
char s[M];
double sum[M] , tot[M];
int main() {
sum[0] = 0.0;
for(int i = 1 ; i < M ; i++) {
sum[i] = 1.0 / i + sum[i - 1];
}
tot[0] = 0.0;
for(int i = 1 ; i < M ; i++) {
tot[i] = sum[i] + tot[i - 1];
}
scanf("%s" , s + 1);
int len = strlen(s + 1);
double ans = 0;
for(int i = 1 ; i <= len ; i++) {
if(s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U' || s[i] == 'Y') {
ans += sum[i];
ans += tot[len] - tot[len - i] - tot[i];
}
}
printf("%.7lf\n" , ans);
return 0;
}

codeforces 509 E. Pretty Song(前缀和+前缀和的前缀和)的更多相关文章

  1. Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和

    题目链接:http://codeforces.com/problemset/problem/282/E E. Sausage Maximization time limit per test 2 se ...

  2. Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)

    题意:你的手机有\(n\)个app,每个app的大小为\(a_i\),现在你的手机空间快满了,你需要删掉总共至少\(m\)体积的app,每个app在你心中的珍惜值是\(b_i\),\(b_i\)的取值 ...

  3. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  4. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

  5. Codeforces Round #624 (Div. 3) C. Perform the Combo(前缀和)

    You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...

  6. Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀

    D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...

  8. codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组

    题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...

  9. Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)

    A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. Spring Boot中自定义注解+AOP实现主备库切换

    摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...

  2. python利用select实现的Socket Server

    # 利用python的select模块实现简单的Socket Sever #实现多用户访问,再次基础上可以实现FTP Server应用程序 # 发布目的,在于解决了客户端强行终止时,服务器端也跟着程序 ...

  3. SWT 注意事项

    一:GridData (1) 将 GridData 的 widthHint 设置为0,可以解决控件大小会随着这其默认值长度大小而改变的问题.

  4. 以太坊RLPx传输协议

    本文主要内容翻译自:The RLPx Transport Protocol,其中添加了一些个人的理解,由于密码学水平有限,不正确之处望指正.另外原文可能已经更新,最新内容请直接阅读原文. 本文档定义了 ...

  5. 商贸型企业 Java 收货 + 入库 + 生成付款单

    package cn.hybn.erp.modular.system.service.impl; import cn.hybn.erp.core.common.page.LayuiPageFactor ...

  6. Of efficiency and methodology

    There are only too many articles and books which pertains to the discussion of efficiency and method ...

  7. canvas 使用图片跨域问题

    项目中需要生成海报,使用了前端生成图片的插件,将背景图,详情图,以及部分的文字说明放在一块并且生成一张新的图片,大体看了一下源码是通过canvas来实现的,在本地的时候完全没有问题,提交到服务器之后就 ...

  8. Flink Metrics 源码解析

    Flink Metrics 有如下模块: Flink Metrics 源码解析 -- Flink-metrics-core Flink Metrics 源码解析 -- Flink-metrics-da ...

  9. .NET World——gPRC概览

    什么是gRPC 官方的定义: gRPC is a modern open source high performance RPC framework that can run in any envir ...

  10. 右键新建 .md

    右键新建 .md 文件 声明:虽然我成功了,并且右键出来了两个,但是在添加 .html 的过程中又失败了,找不到解决办法. win + r --> regedit --> enter 点击 ...