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.
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 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.
abc
1.000000000000000
zzz
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;
}