题目链接: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. codeforces 340 A. The Wall

    水水的一道题,只需要找xy的最小公倍数,然后找a b区间有多少个可以被xy的最小公倍数整除的数,就是答案. //============================================ ...

  2. Java的自动装箱/拆箱

    概述 自JDK1.5开始, 引入了自动装箱/拆箱这一语法糖, 它使程序员的代码变得更加简洁, 不再需要进行显式转换.基本类型与包装类型在某些操作符的作用下, 包装类型调用valueOf()方法将原始类 ...

  3. PythonDay03

    ## 第三章 ### 今日内容 1.整型 2.布尔值 3.字符串 ​ 索引​ 切片​ 步长​ 字符串的方法 4.for循环 ### 1.整型 - python3:全部是整形- python2:整形,长 ...

  4. 关于AJAX的跨域问题

    最近过年的这几天在做毕业设计的时候遇到了一个关于AJAX的跨域问题,本来我是想要用一下聚合数据平台提供的天气预报的接口的,然后做一个当地的天气情况展示,但是在使用AJAX的时候,被告知出现错误了. 这 ...

  5. 1和new Number(1)有什么区别

    1和new Number(1)有什么区别 author: @Tiffanysbear 总结,两者的区别就是原始类型和包装对象的区别. 什么是包装对象 对象Number.String.Boolean分别 ...

  6. 映射&集合

    哈希函数 通过哈希表可以实现 O(1) 复杂度的查找. 哈希函数构造方法:设计好的哈希函数的两个基本原则,计算简单+分布均匀 1. 直接定址法 用key自身的某个线性函数来定址,f(key) = a* ...

  7. nginx在线与离线安装

    1.场景描述 项目要部署到新的服务器上,需要安装nginx,刚好安全部门通知了nginx存在安全漏洞(Nginx整数溢出漏洞,nginx1.13.2之后的版本无问题),就下载最新的nginx进行了安装 ...

  8. 弃用 wget, 拥抱多线程下载 axel

    0x00 事件 对于在 Linux 的下载工具而言,比较常用的就是 wget 或者 curl,吾也一直用 wget 的方式进行网络上的资源下载.偶然发现了 axel 这个支持多线程的下载工具,试用了几 ...

  9. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  10. 5.MySQL数据库操作步骤

    第一步:登录到MySQL服务器 第二步:选择当前要操作的数据库 第三步:设置请求和返回数据的字符集 第四步:执行SQL语句 l 增加记录:INSERT INTO news(title,content) ...