Codeforces 509E 想法
来源:互联网 发布:php进销存管理系统 编辑:程序博客网 时间:2024/04/27 10:57
When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title.
Let's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.
Let's define the prettiness of a word as the sum of simple prettiness of all the substrings of the word.
More formally, let's define the function vowel(c) which is equal to 1, if c is a vowel, and to 0 otherwise. Let si be the i-th character of string s, and si..j be the substring of word s, staring at the i-th character and ending at the j-th character (sisi + 1... sj, i ≤ j).
Then the simple prettiness of s is defined by the formula:
The prettiness of s equals
Find the prettiness of the given song title.
We assume that the vowels are I, E, A, O, U, Y.
The input contains a single string s (1 ≤ |s| ≤ 5·105) — the title of the song.
Print the prettiness of the song with the absolute or relative error of at most 10 - 6.
IEAIAIO
28.0000000
BYOB
5.8333333
YISVOWEL
17.0500000
In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, theprettiness of the song equals to 28.
题意:
定义一个字符串的优美度为这个字符串中元音字母所占的比例。
给定一个字符串,对这个字符串所有子串的优美度求和。
题解:我们算每个字符对所有子串的贡献 很容易得出这样的结果
1+1/2+1/3+1/4
1/2+1/3+1/4+1/5
1/3+1/4+1/5+1/6
那么问题就变成了求解这个平行四边形的和
预处理三角形的阶层和
把下面补齐 上面补齐 变成一个直角三角形
那么就是拿大三角形的阶层和减去上面的再减去左下角的
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[500005];double sum[500005];int len;void init(){int i=1;double d=0;for(i=1;i<=500000;i++){d+=1.0/i;sum[i]=sum[i-1]+d;}}int judge(char t){if(t=='A'||t=='I'||t=='E'||t=='U'||t=='O'||t=='Y')return 1;return 0;}double solve(int t){return sum[len]-sum[len-t]-sum[t-1];//整个-上面-左下角}int main(){init();scanf("%s",s+1);len=strlen(s+1);int i;double ans=0;for(i=1;i<=len;i++){if(judge(s[i])){ans+=solve(i);}}printf("%.8f\n",ans);return 0;}
- Codeforces 509E 想法
- codeForces 509E 想法题
- Pretty Song - CodeForces 509 E 想法题
- Codeforces 852 E Casinos and travel [想法]
- Codeforces 509E
- Codeforces 509E(思维)
- Codeforces Round #379 (Div. 2)E. Anton and Tree(dfs缩点,想法题)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song(数学,想法题,好题)
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- Codeforces Round #397 E. Tree Fold(bfs,想法题,好题)
- Codeforces Round #398 (Div. 2) E. Change-free(想法题,贪心,好题)
- Codeforces 509E. Pretty Song
- Codeforces 229C 想法
- Codeforces 763B 想法
- Codeforces 776C 想法
- Codeforces 359D 想法
- Codeforces 496D 想法
- Codeforces 156B 想法
- Xshell常用命令
- 第五章 语句
- 多项式求逆
- Java数据结构总结
- LeetCode 64 --- Minimum Path Sum
- Codeforces 509E 想法
- QGis依赖的第三方库和工具简介
- 环信SDK 头像、昵称、表情自定义和群聊设置的实现 一(附源码)
- LeetCode 61. Rotate List
- 【OpenCV学习笔记】三、操作像素
- oracle交换分区swap不足
- SpringMVC之Ambiguous mapping(模棱两可的映射)
- QGis的源代码下载以及windows平台使用脚本自动化编译
- 移动前端不得不了解的HTML5 head 头标签(2016最新版)