http://acm.hdu.edu.cn/showproblem.php?pid=4865
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.
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?
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 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)
Sample Input
1 3 Dry Damp Soggy
Sample Output
Case #1: Sunny Cloudy Rainy
/**
题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的依次天气情况。
解题思路:概率dp。dp[i][j]表示i天天气为j的概率,状态转移方程dp[i][j] = max(dp[i][j], dp[i-1][k]*p2[k][j]*p1[j][t] )
p1为天气对叶子湿度影响的概率,p2为昨天天气对今天天气影响的概率
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <stack>
#include <vector>
using namespace std;
double p1[3][4]= {0.6,0.2,0.15,0.05,0.25,0.3,0.2,0.25,0.05,0.1,0.35,0.5};
double p2[3][3]= {0.5,0.375,0.125,0.25,0.125,0.625,0.25,0.375,0.375};
double dp[56][6];
int pre[56][6],n;
int get_num(char s[])
{
if(strcmp(s,"Dry") == 0) return 0;
if(strcmp(s,"Dryish") == 0) return 1;
if(strcmp(s,"Damp") == 0) return 2;
return 3;
}
map<int,string> mp;
void print(int x)
{
if(pre[n][x]==-1)
{
cout << mp[x]<<endl;
return;
}
print(pre[n--][x]);
cout << mp[x]<<endl;
}
int main()
{
int T,tt=0;
mp[0] = "Sunny";
mp[1] = "Cloudy";
mp[2] = "Rainy";
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
memset(pre,-1,sizeof(pre));
scanf("%d",&n);
char s[12];
scanf("%s",s);
int t=get_num(s);
dp[1][0]=0.63*p1[0][t];
dp[1][1]=0.17*p1[1][t];
dp[1][2]=0.2*p1[2][t];
for(int i=2; i<=n; i++)
{
scanf("%s",s);
t=get_num(s);
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
{
double temp=dp[i-1][k]*p1[j][t]*p2[k][j];
if(temp>dp[i][j])
{
dp[i][j]=temp;
pre[i][j]=k;
}
}
}
double temp=0;
int k;
for(int i=0;i<3;i++)
if(dp[n][i]>temp)
{
k=i;
temp=dp[n][i];
}
printf("Case #%d:\n",++tt);
/* stack <string> q;
while(n)
{
q.push(mp[k]);
k=pre[n][k];
n--;
}
while(!q.empty())
{
cout<<q.top()<<endl;
q.pop();
}*/
print(k);
}
return 0;
}