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

思路:拿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);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值