Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song

E. Pretty Song
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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... sji ≤ 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.

Input

The input contains a single string s (1 ≤ |s| ≤ 5·105) — the title of the song.

Output

Print the prettiness of the song with the absolute or relative error of at most 10 - 6.

Sample test(s)
input
IEAIAIO
output
28.0000000
input
BYOB
output
5.8333333
input
YISVOWEL
output
17.0500000
Note

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值