Pretty Song - CodeForces 509 E 想法题
来源:互联网 发布:数据质量管理平台 编辑:程序博客网 时间:2024/04/29 17:36
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
思路:拿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);}
- Pretty Song - CodeForces 509 E 想法题
- Codeforces 509E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song(数学,想法题,好题)
- 【水题】Codeforces Round #289 509E Pretty Song
- codeforces 509E Pretty Song [递推]【杂类】
- 数学题 暴力字符串 Codeforces div2 E. Pretty Song
- CF #289 E. Pretty Song
- codeForces 509E 想法题
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E.Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces 509E 想法
- 12-30 Wannafly每日一题 Pretty Song
- 前缀和 Codeforces509E Pretty Song
- CF509E:Pretty Song(规律,前缀和)
- 输出金字塔数组
- 显示调用构造函数和析构函数
- 出国留学问题
- 用O(nlogn)的算法实现最大上升子序列(LIS)输出.
- 【年终总结】--To My SB English learning road
- Pretty Song - CodeForces 509 E 想法题
- 填数字游戏求解
- 在struts.xml中,如何配置请求转发和请求重定向!
- 命名规范
- Json字符串转换为java对象
- TCP_DEFER_ACCEPT和SO_REUSEPORT
- 基于opencv实现车牌号识别之失败作品
- SVN学习笔记
- 解说==/system/vold/