Codeforces 100187B:A Lot of Joy(數學期望)

B. A Lot of Joy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two boys Gena and Petya wrote on two strips of paper the same string s that consisted of lowercase Latin letters. Then each boy took one strip, cut it into small pieces so that each piece contained exactly one letter and put all pieces into his pocket. After that Gena and Petya repeated the following procedure until their pockets became empty: they took one piece from their pockets and rejoiced if the letters on these pieces were equal.

Determine the expected number of times the boys rejoiced during this process.

Input

The input contains the only string s which was initially written on Gena's and Petya's strips. The string has length between1 and200000, inclusively, and consists of lowercase Latin letters.

Output

Output the only real number — the expected number of times Gena and Petya rejoiced during their business. The absolute or relative error of the answer should not exceed10 - 9.

Examples
Input
abc
Output
1.000000000000000
Input
zzz
Output
3.000000000000000
題目大意:給你一個字符串,然後把這些字符串弄成兩份,一人一份,每個人把所得的字符串都撕成單個的字母,倆人把斯好的紙片放進口袋。然後兩個人同時拿出來一片
紙片,若兩人所拿出的紙片上的字母一樣,那麼愉悅度+1,這樣進行下去,直到倆人口袋的紙片都沒了爲止。求愉悅度的期望值。

代碼如下:
#include <cstdio>
#include <cstring>
char s[200010];
int cnt[30];
int main()
{
	scanf("%s",&s);
	int n=strlen(s);
	for(int i=0;i<n;i++)
	{
		cnt[s[i]-'a']++;
	}
	double ans=0.0;
	for(int i=0;i<n;i++)
	{
		ans=ans+(double)cnt[s[i]-'a'];
	}
	printf("%.9lf\n",ans/(double)n);
	return 0;
 } 


發佈了277 篇原創文章 · 獲贊 20 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章