题解:P11873 最大拟合
最大拟合
题意
只有小学数学知识的我看不懂一点。
记叫声为
简单来说就是构造一些三元组
解法
如果直接乘,精度损失也会导致算出了答案求不出对数。而题中要求输出对数就是一个提示,即把乘法操作换成对数操作。则
我们可以把它写成
三元时同理,结论与二元类似。设其系数为
Code
#include <iostream>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <bitset>
#include <math.h>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define fst first
#define scd second
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector <int>
#define pii pair <int, int>
#define sz(x) ((int)x.size())
#define ms(f, x) memset(f, x, sizeof(f))
#define L(i, j, k) for (int i=(j); i<=(k); ++i)
#define R(i, j, k) for (int i=(j); i>=(k); --i)
#define ACN(i, H_u) for (int i=H_u; i; i=E[i].nxt)
using namespace std;
template <typename INT> void rd(INT &res) {
res=0; bool f=false; char ch=getchar();
while (ch<'0'||ch>'9') f|=ch=='-', ch=getchar();
while (ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch^48), ch=getchar();
res=(f?-res:res);
}
template <typename INT, typename...Args>
void rd(INT &x, Args &...y) { rd(x), rd(y...); }
//dfs
const int maxn=1e5;
const int N=maxn+10;
char s[N]; int n, a[N], cnt[2][2][3];
//wmr
//incra
//lottle
signed main() {
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
scanf("%s", s+1); n=strlen(s+1);
L(i, 1, n) a[i]=(s[i]=='H'?0:1); a[++n]=2;
L(i, 1, n-2) ++cnt[a[i]][a[i+1]][a[i+2]];
db ans=0;
L(i, 0, 1) L(j, 0, 1) {
int x=cnt[i][j][0], y=cnt[i][j][1], z=cnt[i][j][2];
if (x+y+z) {
if (x) ans+=x*log(1.0*x/(x+y+z));
if (y) ans+=y*log(1.0*y/(x+y+z));
if (z) ans+=z*log(1.0*z/(x+y+z));
}
}
printf("%.6lf\n", ans);
return 0;
}