信息熵及其应用
信息量
在生活中,我们总会收到不少信息,这些信息的信息量有所不同。
这里用
我们需要构造一种方式,来描述一个信息的信息量,使得对于两个相互独立(
- “
A 事件成立”的信息量,加上“B 事件成立”的信息量,等于“A,B 事件均成立”的信息量,即I(AB)=I(A)+I(B) 。 - 若
P(A)<P(B) ,则“A 事件成立”的信息量大于“B 事件成立”的信息量,即I(A)>I(B) 。
利用数学知识,可以想到
由于计算机是用二进制存储信息的,所以
信息熵
假设有
为了衡量“谁获胜了”这个问题的价值,我们需要计算这个问题所获信息的期望信息量。
由信息量公式,可以得到期望信息量为:
我们将
例题 1 :P8079 [WC2022] 猜词
你在玩一种类似 Wordle 的游戏。你需要猜出一个长度为
5 的单词,但游戏开始前你会得知单词的首字母。每次询问一个长度为
5 的单词(注意必须是单词),每个位置返回G(字母正确)、S(字母在单词中,返回值不为G的位置出现过,但位置错误)或-(上述两条均不成立)。如果你询问了
1,2,3,4,5 次,你分别会获得150,120,100,95,85 的分数。超过5 次则会获得0 分。在1000 次游戏中,若分数的平均值为x ,则你的得分为\min(100,x) 。
算法 1
假设你不了解信息熵,每次从可能成为答案的单词集合
这样可以拿
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define pii pair<int,int>
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define ro(i,r,l) for(int i=r;i>=l;i--)
typedef long long ll;
vector<string>_word,word,rword;
mt19937 rnd(time(0));
void init(int n,const char *s){
fo(i,0,n-1){
string wd="";
fo(j,i*5,i*5+4)
wd+=s[j];
_word.push_back(wd);
}
}
string lst;
const char *guess(int Case,int Qtimes,char c0,bool *gold,bool *silver){
if (Qtimes==5){
word.clear();
for (auto i:_word)
if (i[0]==c0)
word.push_back(i);
}
else{
rword.clear();
for (auto i:word){
bool sb=1;
fo(j,0,4){
if (gold[j]^(i[j]==lst[j]))
sb=0;
if (silver[j]){
bool sb2=0;
fo(k,0,4)
if (i[k]==lst[j])
sb2=1;
sb&=sb2;
}
}
if (sb)
rword.push_back(i);
}
word=rword;
}
int x=rnd()%word.size();
lst=word[x];
return lst.c_str();
}
:::
算法 2
随机选取肯定是不优秀的,所以我们需要尽可能地提升询问的价值。
也就是说,尽可能地提升询问的信息熵。
考虑计算
可惜时间消耗过大,一分钟也不足以容纳。
算法 3
注意到时间消耗的瓶颈在第一次询问,而第一次询问时已有的信息(首字母)只有
这样时间消耗就不是问题了,可以拿到
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define pii pair<int,int>
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define ro(i,r,l) for(int i=r;i>=l;i--)
typedef long long ll;
const string bests[]={
"slier","lares","lares","tores","tarns","arles","lares","lares","snare",
"ousel","ranis","nares","tares","aides","tries","lares","raise","aides",
"plate","nares","snare","riles","nares","cones","kanes","aeons"
};
vector<string>_word,word,rword;
mt19937 rnd(time(0));
void init(int n,const char *s){
fo(i,0,n-1){
string wd="";
fo(j,i*5,i*5+4)
wd+=s[j];
_word.push_back(wd);
}
}
int c[26];
int checker(string out,string ans){
int rs=0,p=1;
fo(i,0,25)
c[i]=0;
fo(i,0,4)
if (out[i]!=ans[i])
c[ans[i]-'a']++;
fo(i,0,4){
if (out[i]==ans[i])
rs+=p;
else if (c[out[i]-'a'])
rs+=2*p;
p*=3;
}
return rs;
}
int cnt[243];
string lst;
double Hcalc(string str){
double rs=0;
fo(S,0,242)
cnt[S]=0;
for (auto i:word)
cnt[checker(str,i)]++;
fo(S,0,242){
double p=cnt[S];
p/=word.size();
if (p)
rs-=p*log2(p);
}
return rs;
}
const char *guess(int Case,int Qtimes,char c0,bool *gold,bool *silver){
if (Qtimes==5){
word.clear();
for (auto i:_word)
if (i[0]==c0)
word.push_back(i);
lst=bests[c0-'a'];
return lst.c_str();
}
int s=0,p=1;
fo(i,0,4){
s+=p*gold[i];
s+=2*p*silver[i];
p*=3;
}
rword.clear();
for (auto i:word)
if (checker(lst,i)==s)
rword.push_back(i);
word=rword;
double mxH=-1;
for (auto i:word){
double H=Hcalc(i);
if (mxH<H)
lst=i,mxH=H;
}
return lst.c_str();
}
:::
算法 4
考虑一种情况:
例如答案单词为 foots,候选单词集合 foods、fools、foots(注意到这三个单词只有第四位不一样),那么一种最劣的情况需要分别询问 foods、fools、foots 才能得到答案(有时候可能有
但如果此时询问一次 delta(同时包含 d、l、t),就一定可以得到答案。
所以,询问不能局限于
假设单词表为
但这样会 TLE,考虑到遇到这种情况时
这样就能通过了。
算法 5
不过,据说的话,对一个字符串
取
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define pii pair<int,int>
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define ro(i,r,l) for(int i=r;i>=l;i--)
typedef long long ll;
const string bests[]={
"slier","lares","lares","tores","tarns","arles","lares","lares","snare",
"ousel","ranis","nares","tares","aides","tries","lares","raise","aides",
"plate","nares","snare","riles","nares","cones","kanes","aeons"
};
vector<string>_word,word,rword;
mt19937 rnd(time(0));
void init(int n,const char *s){
fo(i,0,n-1){
string wd="";
fo(j,i*5,i*5+4)
wd+=s[j];
_word.push_back(wd);
}
}
int c[26];
int checker(string out,string ans){
int rs=0,p=1;
fo(i,0,25)
c[i]=0;
fo(i,0,4)
if (out[i]!=ans[i])
c[ans[i]-'a']++;
fo(i,0,4){
if (out[i]==ans[i])
rs+=p;
else if (c[out[i]-'a'])
rs+=2*p;
p*=3;
}
return rs;
}
int cnt[243];
string lst;
double Hcalc(string str){
double rs=0;
fo(S,0,242)
cnt[S]=0;
for (auto i:word)
cnt[checker(str,i)]++;
fo(S,0,242){
double p=cnt[S];
p/=word.size();
if (p)
rs-=p*log2(p);
}
return rs;
}
const char *guess(int Case,int Qtimes,char c0,bool *gold,bool *silver){
if (Qtimes==5){
word.clear();
for (auto i:_word)
if (i[0]==c0)
word.push_back(i);
lst=bests[c0-'a'];
return lst.c_str();
}
int s=0,p=1;
fo(i,0,4){
s+=p*gold[i];
s+=2*p*silver[i];
p*=3;
}
rword.clear();
for (auto i:word)
if (checker(lst,i)==s)
rword.push_back(i);
word=rword;
double mxH=-1;
for (auto i:word){
double H=Hcalc(i)+0.01;
if (mxH<H)
lst=i,mxH=H;
}
if (word.size()<=50){
for (auto i:_word){
double H=Hcalc(i);
if (mxH<H)
lst=i,mxH=H;
}
}
return lst.c_str();
}
:::
参考
- deepseek 对话。
- 浅谈信息论 by bsTiat。
- P8079 题解 by aberter0x3f。