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. 1
  2. 3
  3. Dry
  4. Damp
  5. Soggy
 
Sample Output
  1. Case #1:
  2. Sunny
  3. Cloudy
  4. Rainy
  5. Hint
  6. Log is useful.
  7.  
 
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向前推,找出全部的天气就可以。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. double wh[3][4]={0.6,0.2,0.15,0.05,
  9. 0.25,0.3,0.2,0.25,
  10. 0.05,0.1,0.35,0.50};
  11. double ww[3][3]={0.5,0.375,0.125,
  12. 0.25,0.125,0.625,
  13. 0.25,0.375,0.375};
  14.  
  15. int main()
  16. {
  17. int t,cas=1,n,h[55];
  18. char s[10];
  19. cin>>t;
  20. while(t--)
  21. {
  22. cin>>n;
  23. for(int i=0;i<n;i++)
  24. {
  25. scanf("%s",s);
  26. if(strcmp(s,"Dry")==0) h[i]=0;
  27. else if(strcmp(s,"Dryish")==0) h[i]=1;
  28. else if(strcmp(s,"Damp")==0) h[i]=2;
  29. else h[i]=3;
  30. }
  31.  
  32. double dp[55][3],path[55][3],print[55];
  33.  
  34. dp[0][0]=log(0.63*wh[0][h[0]]);
  35. dp[0][1]=log(0.17*wh[1][h[0]]);
  36. dp[0][2]=log(0.20*wh[2][h[0]]);
  37.  
  38. for(int i=1;i<n;i++)
  39. {
  40. for(int j=0;j<3;j++)
  41. {
  42. double max=-1e7;
  43. int v=-1;
  44. for(int k=0;k<3;k++)
  45. if(max<dp[i-1][k]+log(wh[j][h[i]]*ww[k][j]))
  46. {
  47. v=k;
  48. max=dp[i-1][k]+log(wh[j][h[i]]*ww[k][j]);
  49. }
  50. dp[i][j]=max;
  51. path[i][j]=v;
  52. }
  53. }
  54. int k=0;
  55. if(dp[n-1][1]>dp[n-1][k]) k=1;
  56. if(dp[n-1][2]>dp[n-1][k]) k=2;
  57. print[n-1]=k;
  58. for(int i=n-1;i>=1;i--)
  59. {
  60. print[i-1]=path[i][k];
  61. k=print[i-1];
  62. }
  63. cout<<"Case #"<<cas++<<":"<<endl;
  64. for(int i=0;i<n;i++)
  65. {
  66. if(print[i]==0) printf("Sunny\n");
  67. else if(print[i]==1) printf("Cloudy\n");
  68. else printf("Rainy\n");
  69. }
  70. }
  71. return 0;
  72. }

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. JDK1.8源码hashMap

    一.概念 允许key为null,value为null:线程不安全:不保证映射的顺序:迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数) ...

  2. springboot日志框架

    Spring Boot日志框架Spring Boot支持Java Util Logging,Log4j2,Lockback作为日志框架,如果你使用starters启动器,Spring Boot将使用L ...

  3. nor flash 和 nand flash

    NOR Flash是很常见的一种存储芯片,数据掉电不会丢失,支持Execut On Chip,即程序可以直接在FLASH片内执行(这意味着存储在NOR FLash上的程序不需要复制到RAM就可以直接运 ...

  4. google地图的url参数

    Google Maps Intents for Android The Google Maps app for Android exposes several intents that you can ...

  5. webapck编译打包stylus文件

    先安装css-loader.stylus.stylus-loader npm install --save-dev css-loader npm install --save-dev stylus n ...

  6. (转)js函数前加分号和感叹号是什么意思?有什么用?

    转载地址:https://www.cnblogs.com/mq0036/p/4605255.html 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuer ...

  7. Java编程的逻辑 (11) - 初识函数

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  8. JavaScript——HashMap实现

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws . 首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的 ...

  9. 主动学习(Active Learning)

    主动学习简介 在某些情况下,没有类标签的数据相当丰富而有类标签的数据相当稀少,并且人工对数据进行标记的成本又相当高昂.在这种情况下,我们可以让学习算法主动地提出要对哪些数据进行标注,之后我们要将这些数 ...

  10. non linear processor

          由于自适应滤波器只能估算回音的近似值,因此它并不能将回音完全消除,也就是说,仍然有些残余的回音(Lres)留在信号当中.在任何时刻中,暂存器中的准确性正是决定残余回音音量的关键所在.如果残 ...