Problem Description
Recently, Peter likes to measure the humidity of leaves. He recorded a leaf humidity every day. There are four types of leaves wetness: Dry , Dryish , Damp and Soggy. As we know, the humidity of leaves is affected by the weather. And there are only three kinds of weather: Sunny, Cloudy and Rainy.For example, under Sunny conditions, the possibility of leaves are dry is 0.6.
Give you the possibility list of weather to the humidity of
leaves.
Give you the possibility list of weather to the humidity of
leaves.
The weather
today is affected by the weather yesterday. For example, if yesterday is Sunny,
the possibility of today cloudy is 0.375.
The relationship between weather
today and weather yesterday is following by table:
Now,Peter has
some recodes of the humidity of leaves in N days.And we know the weather
conditons on the first day : the probability of sunny is 0.63,the probability of
cloudy is 0.17,the probability of rainny is 0.2.Could you know the weathers of
these days most probably like in order?
Input
The first line is T, means the number of cases, then
the followings are T cases. for each case:
The first line is a integer
n(n<=50),means the number of days, and the next n lines, each line is a
string shows the humidity of leaves (Dry, Dryish, Damp, Soggy)
the followings are T cases. for each case:
The first line is a integer
n(n<=50),means the number of days, and the next n lines, each line is a
string shows the humidity of leaves (Dry, Dryish, Damp, Soggy)
Output
For each test case, print the case number on its own
line. Then is the most possible weather sequence.( We guarantee that the data
has a unique solution)
line. Then is the most possible weather sequence.( We guarantee that the data
has a unique solution)
Sample Input
1
3
Dry
Damp
Soggy
Sample Output
Case #1:
Sunny
Cloudy
Rainy
Hint
Log is useful.
概率dp time :0ms
#include"iostream"
#include"cstdio"
#include"map"
#include"cstring"
#include"string"
#include"algorithm"
#include"vector"
using namespace std;
const int ms=;
double p1[][]= {0.6,0.2,0.15,0.05,0.25,0.3,0.2,0.25,0.05,0.10,0.35,0.50};
double p2[][]= {0.5,0.375,0.125,0.25,0.125,0.625,0.25,0.375,0.375};
double dp[ms][];
int pre[ms][];
map<int,string> m1;
map<string,int> m2;
string str;
void init()
{
m2.insert(make_pair("Dry",));
m2.insert(make_pair("Dryish",));
m2.insert(make_pair("Damp",));
m2.insert(make_pair("Soggy",));
m1.insert(make_pair(,"Sunny"));
m1.insert(make_pair(,"Cloudy"));
m1.insert(make_pair(,"Rainy"));
}
int main()
{
int ncase,T=;
scanf("%d",&ncase);
init();
while(ncase--)
{
printf("Case #%d:\n",++T);
int n,i,j,k;
scanf("%d",&n);
cin>>str;
for(i=;i<=n;i++)
for(j=;j<;j++)
dp[i][j]=;
int lab=m2[str];
memset(pre,,sizeof(pre));
dp[][]=0.63*p1[][lab];
dp[][]=0.17*p1[][lab];
dp[][]=0.2*p1[][lab];
for(i=;i<=n;i++)
{
cin>>str;
lab=m2[str];
for(j=;j<;j++)
for(k=;k<;k++)
{
double pp=dp[i-][k]*p2[k][j]*p1[j][lab];
if(pp>dp[i][j])
{
dp[i][j]=pp;
pre[i][j]=k;
}
}
}
vector<int> ans;
double mi=;
int po;
for(i=;i<;i++)
if(dp[n][i]>mi)
{
mi=dp[n][i];
po=i;
}
ans.push_back(po);
int now=n;
while(now!=)
{
po=pre[now][po];
ans.push_back(po);
now--;
}
for(i=n-;i>=;i--)
{
//printf("%s\n",m1[ans[i]]);
cout<<m1[ans[i]]<<endl;
}
}
return ;
}