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;
}

hdu 4865 Peter&#39;s Hobby的更多相关文章

  1. hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

    Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. hdu 4865 Peter&#39;s Hobby(概率dp)

    http://acm.hdu.edu.cn/showproblem.php? pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的 ...

  3. hdu 4865 Peter&#39;s Hobby (隐马尔可夫模型 dp)

    Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. HDU 4865 Peter's Hobby(概率、dp、log)

    给出2个影响矩阵,一个是当天天气对湿度的影响,一个是前一天天气对当天天气的影响. 即在晴天(阴天.雨天)发生Dry(Dryish.Damp.Soggy)的概率,以及前一天晴天(阴天.雨天)而今天发生晴 ...

  5. HDU 4865 Peter's Hobby --概率DP

    题意:第i天的天气会一定概率地影响第i+1天的天气,也会一定概率地影响这一天的湿度.概率在表中给出.给出n天的湿度,推测概率最大的这n天的天气. 分析:这是引自机器学习中隐马尔科夫模型的入门模型,其实 ...

  6. 2014多校第一场 E 题 || HDU 4865 Peter's Hobby (DP)

    题目链接 题意 : 给你两个表格,第一个表格是三种天气下出现四种湿度的可能性.第二个表格是,昨天出现的三种天气下,今天出现三种天气的可能性.然后给你这几天的湿度,告诉你第一天出现三种天气的可能性,让你 ...

  7. HDU 4865 Peter's Hobby(2014 多校联合第一场 E)(概率dp)

    题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2 ...

  8. HDU 4865 Peter's Hobby

    $dp$. 这题的本质和求一个有向无环图的最长路径长度的路径是一样的. $dp[i][j]$表示到第$i$天,湿度为$a[i]$,是第$j$种天气的最大概率.记录一下最大概率是$i-1$天哪一种天气推 ...

  9. Peter&#39;s Hobby

    主题链接 题意: 题意比較麻烦.. .n天,给出每天的叶子的一种状态(Dry , Dryish , Damp and Soggy),最有可能出现的天气序列(Sunny, Cloudy and Rain ...

随机推荐

  1. 全国大学API接口分享

    1.获取省份列表: http://119.29.166.254:9090/api/provinces 返回的是省份列表,其中id很重要. 2.通过省份id查询省份城市: http://119.29.1 ...

  2. Twisted框架

    Twisted是一个事件驱动型的网络模型.时间驱动模型编程是一种范式,这里程序的执行流由外部决定.特点是:包含一个事件循环,当外部事件发生时,使用回调机制来触发相应的处理. 线程模式: 1.单线程同步 ...

  3. springcloud使用Hystrix实现微服务的容错处理

    使用Hystrix实现微服务的容错处理 容错机制 如果服务提供者相应非常缓慢,那么消费者对提供者的请求就会被强制等待,知道提供者相应超时.在高负载场景下,如果不作任何处理,此类问题可能会导致服务消费者 ...

  4. Java中抽象类概述

    抽象类 1.抽象类的定义 抽象类是为子类提供一个规范,这就必须联系到继承: 抽象类的制定就是让子类继承的:  public abstract 类名{ //类体 //抽象方法 修饰符 abstract ...

  5. java 异常历史 和观点

    异常起源于PL/1和Mesa之类的系统中. 1.) 不在于编译器是否会强制程序员去处理错误,而是要由一致的,使用异常来报告错误 2.) 不在于什么时候进行检查,而是一定要有检查.

  6. SVN使用过程中遇到的一些问题

    更新svn的客户端TortoiseSVN后 ,之前使用svn管理的文件的关联图标消失了 说明:下面的解决方法及图片来自博客:装了SVN,你的关联图标变了没有? 解决办法:在同步的文件点击右键如下图   ...

  7. Tomcat8 启动中提示 org.apache.catalina.webresources.Cache.getResource Unable to add the resource

    Tomcat8 启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xx ...

  8. OpenStack中的Multipath faulty device的成因及解决(part 1)

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 简介: Multip ...

  9. hdu 5003 模拟水题 (2014鞍山网赛G题)

    你的一系列得分 先降序排列 再按0.95^(i-1)*ai 这个公式计算你的每一个得分 最后求和 Sample Input12530 478Sample Output984.1000000000 # ...

  10. 网站目录下多出的 core 文件

    1.core 文件简介 在一个程序崩溃时,一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的.也就是说这种文件是程序意外中断时候生成的 deb ...