CF509E:Pretty Song(规律,前缀和)
来源:互联网 发布:淘宝客服名称 编辑:程序博客网 时间:2024/05/16 01:58
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, the prettiness of the song equals to 28.
大意:给定一个字符串s,计算其中各子串元音字母所占比例,输出比例之和。
假定sum[i]表示1~i中元音字母的个数,len表示字符串总长度,字符串从下标1开始。
对于长度为1的子串,每个元音字母都要用1次,即(sum[len] -sum[0]) / 1,
对于长度为2的子串,s[2]到第s[len-1]的元音字母都要用2次,而第s[1]和s[len]个位置的元音字母只需用1次,即( (sum[len] -sum[0]) + (sum[len-1] - sum[1]) ) / 2,
对于长度为3的子串,s[3]到第s[len-2]的元音字母都要用3次,s[2]到第s[len-1]的元音字母都要用2次,而第s[1]和s[len]个位置的元音字母只需用1次,即( (sum[len] -sum[0]) + (sum[len-1] - sum[1]) + (sum[len-2] - sum[2]) ) / 3,
依此类推,各个长度的结果加起来就是答案。
# include <stdio.h># include <string.h># define MAXN 500000int sum[MAXN+3];int main(){ char c; int len=0, i; double result=0, ans=0; memset(sum, 0, sizeof(sum)); while((c = getchar()) != '\n') { ++len; if(c=='A'||c=='E'||c=='I'||c=='O'||c=='Y'||c=='U') ++sum[len]; sum[len] += sum[len-1]; } for(i=0; i<len; ++i) { ans += sum[len-i] - sum[i]; result += ans / (i+1); } printf("%.7lf\n",result); return 0;}
- CF509E:Pretty Song(规律,前缀和)
- 前缀和 Codeforces509E Pretty Song
- CF509E 找规律
- CF #289 E. Pretty Song
- Codeforces 509E. Pretty Song
- Pretty Song - CodeForces 509 E 想法题
- 【水题】Codeforces Round #289 509E Pretty Song
- 数学题 暴力字符串 Codeforces div2 E. Pretty Song
- 12-30 Wannafly每日一题 Pretty Song
- codeforces 509E Pretty Song [递推]【杂类】
- Codeforecs 424C Magic Formulas 规律+前缀和
- codeforces846C(前缀和)
- Per(前缀和)
- Pretty Poem(substr)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E.Pretty Song
- Android 内存调试工具
- 自定义组合控件的步骤
- Juery 遍历某个方法,使其自动执行多次
- Linux菜鸟的入门-终端环境中C语言编程
- 条件语句和循环
- CF509E:Pretty Song(规律,前缀和)
- 迭代器之map的简单介绍
- 实时监听input值的改变
- 第1、2节-线性规划、梯度下降和正规方程组
- [51nod 1184]第N个质数
- File类
- openCV中访问图像像素的两种方法
- 17 - 03 - 24 图解HTTP(28)
- 顺序表应用7:最大子段和之分治递归法