Pretty Song - CodeForces 509 E 想法题

来源:互联网 发布:数据质量管理平台 编辑:程序博客网 时间:2024/04/29 17:36

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
题意:给你一个字符串,问他所有子串中元音的比例的和是多少。

思路:拿7位的为例,设a[i]为sum(num[i]+...+num[n-i+1])。那么在长度为k的所有子串中,元音出现的次数和为:

        长度为1的 a[1]

        长度为2的 a[2]*2+(a[1]-a[2])

        长度为3的 a[3]*3+(a[2]-a[3])*2+(a[1]-a[2])

        长度为4的 a[4]*4+(a[3]-a[4])*2+(a[2]-a[3])*2+(a[1]-a[2])

        长度为5的 a[3]*3+(a[2]-a[3])*2+(a[1]-a[2])

        长度为6的 a[2]*2+(a[1]-a[2])

        长度为7的 a[1]

 这个是奇数位的,偶数位的可以自己类比着推一下。

AC代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;int T,t,n,m;char s[500010];int num[500010],sum;ll a[500010];double ans;int main(){    int i,j,k,len,ret,l,r,N;    ll p=0,f=0;    double ans=0;    scanf("%s",s+1);    len=strlen(s+1);    for(i=1;i<=len;i++)    {        if(s[i]=='A' || s[i]=='E'|| s[i]=='I'|| s[i]=='O'|| s[i]=='U'|| s[i]=='Y')        {            num[i]=1;            sum++;        }    }    l=1;r=len;ret=sum;    N=(len+1)/2;    for(i=1;i<=N;i++)    {        a[i]=ret;        ret=ret-num[l]-num[r];        l++;r--;    }    if(len&1)      a[N]=num[N];    for(i=1;i<=N;i++)    {        ans+=1.0*(a[i]*i+f)/i;        f+=(a[i]-a[i+1])*i;    }    i--;    f-=(a[i]-a[i+1])*i;    if(len&1){    p=N-1;    for(i=N+1;i<=len;i++)    {        f-=(a[p]-a[p+1])*p;        ans+=1.0*(a[p]*p+f)/i;        p--;    }}    else    {        p=N;        for(i=N+1;i<=len;i++)        {            ans+=1.0*(a[p]*p+f)/i;            f-=(a[p-1]-a[p])*(p-1);            p--;        }    }    printf("%.7f\n",ans);}




0 0
原创粉丝点击