hdu 4865 Peter's Hobby

Peter's Hobby

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 283    Accepted Submission(s): 128


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?
 

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)
 

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
Hint
Log is useful.
 

Author
FZU
 

Source
 


题目:给的信息就是每天树叶的状态和今天天气有一定的关系,明天的天气和今天的天气也有一定的关系,目前给出n天的树叶状态以及第一天三种天气的概率,来求出这n概率最大的天气序列。


题解:比赛的时候也想到了这道题的计算方法,但是觉得不是很合理,当然看了赛后的题解依然觉得不合理,但是还是按照题解做了一下。我觉得不合理之处是题目中给出的是根据天气看树叶状态的概率,而题目中给出的是树叶的状态来推天气状况,这就让大家想到 要求条件概率,而不是简单得相乘-----个人见解,如果觉得说的不对,求指正。

          一条天气链的概率的求法s[a1]*wh[a1][b1]*ww[a1][a2]*wh[a2][b2]*.......*ww[an-1][an]*wh[an][bn];因为乘机太小,所以转化成log来求和,取最大的那条就可以了。

         因为每天只有三种天气状态,使用dp[i][j]来记录从第一天到第i天的概率和(第i天为第j种天气),过程中使用path[i][j]记录前一天的天气情况。最后找出最后一天概率和最大的天气,根据path向前推,找出所有的天气即可。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

double wh[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.50};
double ww[3][3]={0.5,0.375,0.125,
                 0.25,0.125,0.625,
                 0.25,0.375,0.375};

int main()
{
    int t,cas=1,n,h[55];
    char s[10];
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            if(strcmp(s,"Dry")==0) h[i]=0;
            else if(strcmp(s,"Dryish")==0) h[i]=1;
            else if(strcmp(s,"Damp")==0) h[i]=2;
            else h[i]=3;
        }

        double dp[55][3],path[55][3],print[55];

        dp[0][0]=log(0.63*wh[0][h[0]]);
        dp[0][1]=log(0.17*wh[1][h[0]]);
        dp[0][2]=log(0.20*wh[2][h[0]]);

        for(int i=1;i<n;i++)
        {
            for(int j=0;j<3;j++)
            {
                double max=-1e7;
                int v=-1;
                for(int k=0;k<3;k++)
                if(max<dp[i-1][k]+log(wh[j][h[i]]*ww[k][j]))
                {
                   v=k;
                   max=dp[i-1][k]+log(wh[j][h[i]]*ww[k][j]);
                }
                dp[i][j]=max;
                path[i][j]=v;
            }
        }
        int k=0;
        if(dp[n-1][1]>dp[n-1][k]) k=1;
        if(dp[n-1][2]>dp[n-1][k]) k=2;
        print[n-1]=k;
        for(int i=n-1;i>=1;i--)
        {
            print[i-1]=path[i][k];
            k=print[i-1];
        }
        cout<<"Case #"<<cas++<<":"<<endl;
        for(int i=0;i<n;i++)
        {
            if(print[i]==0) printf("Sunny\n");
            else if(print[i]==1) printf("Cloudy\n");
            else printf("Rainy\n");
        }
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值