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
首先对于每个字符,在字符串的子串中出现只有3种形式(如果将“单个字符为一个子串”这种特殊情况
分出来也可以说是4种):分别是 以该字符开头,以该字符结尾,该字符在子串中间。
该字符的位置为i,字符串从0~len-1。长度为len。
1、以该字符结尾
1/1+1/2+1/3+1/4+.....+1/(i+1)
2、以该字符开头
1/1+1/2+1/3+1/4+.....+1/(len-i)
这里,单个字符的情况在1、2中重复了,故应-1去重
3、该字符在中间的情况
下标 0,1,2,.... ,i-1,i,i+1,....,len-1
以i-1开头,结尾>=i+1: 1/3+1/4+....+1/(len-i+1) ,即sum[len-i+1]-sum[2]
以i-2开头,结尾>=i+1: 1/4+1/5+....+1/(len-i+2) ,即sum[len-i+2]-sum[3]
................
以0开头,结尾>=i+1: 1/(i+2)+1/(i+3)+....+1/len ,即sum[len]-sum[i+1]
上式累加得:(sum[len-i+1]+sum[len-i+2]+...+sum[len]) - (sum[2]+sum[3]+...+sum[i+1])
即t[len] - t[len-i] - ( t[i+1] - t[1] )
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
char str[500010];
double sum[500010],t[500010];
void init()
{
memset(sum,0,sizeof(sum));
memset(t,0,sizeof(t));
for(int i=1;i<=500000;i++)
sum[i] = 1.0/i+sum[i-1];
for(int i=1;i<=500000;i++)
t[i] = sum[i]+t[i-1];
}
bool check(char c)
{
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='Y')
return true;
return false;
}
int main()
{
init();
while(~scanf("%s",str))
{
int len = strlen(str);
double ans = 0.0;
for(int i=0;i<len;i++)
{
if(check(str[i]))
{
ans += sum[i+1]+sum[len-i]-1;
ans += t[len]-t[len-i]-t[i+1]+t[1];
}
}
printf("%.7lf\n",ans);
}
return 0;
}