codeforces 509 E. Pretty Song(前缀和+前缀和的前缀和)
题目链接: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(前缀和+前缀和的前缀和)的更多相关文章
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和
题目链接:http://codeforces.com/problemset/problem/282/E E. Sausage Maximization time limit per test 2 se ...
- Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)
题意:你的手机有\(n\)个app,每个app的大小为\(a_i\),现在你的手机空间快满了,你需要删掉总共至少\(m\)体积的app,每个app在你心中的珍惜值是\(b_i\),\(b_i\)的取值 ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组
题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...
- 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 ...
随机推荐
- Go组件学习——gorm四步带你搞定DB增删改查
1.简介 ORM Object-Relationl Mapping, 它的作用是映射数据库和对象之间的关系,方便我们在实现数据库操作的时候不用去写复杂的sql语句,把对数据库的操作上升到对于对象的操作 ...
- lvs+keepalived 高可用及负载均衡
一.环境准备 VIP:10.18.43.30 dr1:10.18.43.10 dr2:10.18.43.20 web1:10.18.43.13 web2:10.18.43.14 结构图 (一).预处理 ...
- 关于Java虚拟机运行时数据区域的总结
Java虚拟机运行时数据区域 程序计数器(Program Counter) 程序计数器作为一个概念模型,这个是用来指示下一条需要执行的字节码指令在哪. Java的多线程实际上是通过线程轮转做到的,如果 ...
- springmvc异步处理
好久没有写过博客了,都是看大牛的文章,略过~~ 突然感觉成长在于总结!废话不多说,开干 由于是公司项目,所以不方便给出代码,看图操作 在项目util目录下创建工具类TaskExecutorConfig ...
- c语言ld returned 1 exit status😂
在复习c语言过程中遇到, 问题:reverseLinkedList.exe: Permission denied collect2.exe: error: ld returned 1 exit sta ...
- 1.2模板templates
一.模板使用 1. 配置模板目录 如果命令行创建的项目,需要手动配置模板文件目录,如果是Pycharm创建的项目,则无需配置 在项目根目录下创建模板目录,比如叫 templates,后续开发模板文件会 ...
- 帝国CMS(EmpireCMS) v7.5后台任意代码执行
帝国CMS(EmpireCMS) v7.5后台任意代码执行 一.漏洞描述 EmpireCMS 7.5版本及之前版本在后台备份数据库时,未对数据库表名做验证,通过修改数据库表名可以实现任意代码执行. 二 ...
- MySQL-InnoDB锁(二)
上篇文章中对InnoDB存储引擎中的锁进行学习,本文是实践部分,根据索引和查询范围,探究加锁范围的情况. 在本实例中,创建简单表如下: mysql> select * from t; +---- ...
- Spring源码剖析开篇:什么是Spring?
在讲源码之前,先让我们回顾一下一下Spring的基本概念,当然,在看源码之前你需要使用过spring或者spirngmvc. Spring是什么 Spring是一个开源的轻量级Java SE(Java ...
- Swoole引擎原理的快速入门干货
更多内容,欢迎关注微信公众号:全菜工程师小辉~ 过去一年使用PHP和Java两种技术栈完成了一个游戏服务器项目.由于项目中有高频的网络请求,所以PHP技术栈尝试使用Swoole引擎(基于事件的高性能异 ...