标签:1.0 char s -- char printf main define clu push
\(n\):表示字符的种类;
\(a_i\):表示单词i的个数;
\(Len\):表示串的长度;
先考虑一种单词\(a_i\)放在一个固定位置p对答案的贡献:
整一个串的全排类为:\[P = \frac{Len!}{a_1!a_2!...a_n!}\]
固定\(a_i\)的排列为:\[P_i = \frac{(Len-1)!}{a_1!a_2!...(a_i-1)!..a_n!}\]
对于最终答案贡献就是:\[ans_{i,p} = \frac{P_i^2}{P^2}\]
则有:\[ans_i = \sum_{p=1}^{Len} ans_{i,p} = \frac{Len P_i^2}{P^2} = \frac{a_i^2}{Len}\]
下面只需枚举字符求和即可:\[ans = \sum_{i=1}^{n}ans_i = \sum_{i=1}^{n} \frac{a_i^2}{Len}\]
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define PII pair<int,int>
typedef long long ll;
const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
using namespace std;
int n,k,M[666],num;
char s[200007];
double ans = 0;
int main() {
scanf(" %s",s+1);
n=strlen(s+1);
rep(i,1,n) ++M[s[i]-‘a‘];
rep(i,0,25) {
ans += (1.0*M[i]*M[i])/n;
}
printf("%.10f\n",ans);
return 0;
}
标签:1.0 char s -- char printf main define clu push
原文地址:https://www.cnblogs.com/RRRR-wys/p/9460637.html