Codeforces 509E 想法

本文介绍了一种计算歌曲标题优美度的方法,通过定义优美度为元音字母比例,并对所有子串的优美度求和来评估歌曲标题的整体美感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Examples
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+1/2+1/3+1/4
     1/2+1/3+1/4+1/5
             1/3+1/4+1/5+1/6

那么问题就变成了求解这个平行四边形的和

预处理三角形的阶层和

把下面补齐  上面补齐  变成一个直角三角形

那么就是拿大三角形的阶层和减去上面的再减去左下角的


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[500005];
double sum[500005];
int len;
void init(){
	int i=1;
	double d=0;
	for(i=1;i<=500000;i++){
		d+=1.0/i;
		sum[i]=sum[i-1]+d;
	}
}
int judge(char t){
	if(t=='A'||t=='I'||t=='E'||t=='U'||t=='O'||t=='Y')return 1;
	return 0;
}
double solve(int t){
	return sum[len]-sum[len-t]-sum[t-1];//整个-上面-左下角
}
int main(){
	init();
	scanf("%s",s+1);
	len=strlen(s+1);
	int i;
	double ans=0;
	for(i=1;i<=len;i++){
		if(judge(s[i])){
			ans+=solve(i);
		}
	}
	printf("%.8f\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值