题目链接: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. python 处理json数据

    python 处理 json数据 以下是登录账号后获取的json数据,headers中注意加入cookie值 需要处理的数据如下: 全部代码如下 #!/usr/bin/env python # -*- ...

  2. HttpClientFactory 使用说明 及 对 HttpClient 的回顾和对比

    目录 HttpClient 日常使用及坑点: HttpClientFactory 优势: HttpClientFactory 使用方法: 实战用法1:常规用法 1. 在 Startup.cs 中进行注 ...

  3. 10分钟了解一致性hash算法

    应用场景 当我们的数据表超过500万条或更多时,我们就会考虑到采用分库分表:当我们的系统使用了一台缓存服务器还是不能满足的时候,我们会使用多台缓存服务器,那我们如何去访问背后的库表或缓存服务器呢,我们 ...

  4. JavaFX 选择文件 导入Excel文件并解析

    FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...

  5. 分布式ID系列(3)——数据库自增ID机制适合做分布式ID吗

    数据库自增ID机制原理介绍 在分布式里面,数据库的自增ID机制的主要原理是:数据库自增ID和mysql数据库的replace_into()函数实现的.这里的replace数据库自增ID和mysql数据 ...

  6. JDK、JRE、JVM之间的区别和联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  7. MySQL多表(理论知识总结)

    1. 多表关系    外键    foreign key    添加外键语法:    alter table 表名1 add foreign key(外键名称) references 表名2(主键名称 ...

  8. python3学习-Queue模块

    python标准库中带有一个Queue模块,顾名思义,队列.该模块也衍生出一些基本队列不具有的功能. 我们先看一下队列的方法: put 存数据 get 取数据 empty 判断队列是否为空 qsize ...

  9. JavaWeb——使用会话维持状态2

    在这次的例子里面,将完成一类似购物车的功能,在客户访问网站的时候,会选中自己将要购买的商品,而购物车将始终维持着商品的状态,会话将联系起选择第一个商品(第一个请求),选择其他商品(其他请求)以及付款等 ...

  10. 析构函数中调用 Dispose 报错 :Internal .Net Framework Data Provider error 1.[非原创]

    搜索MSDN的资源可以找到答案: 原文如下http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=473449&SiteID=1 以下是关于 ...