Peter's Hobby

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 545    Accepted Submission(s): 237

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?


题目大意:有三种天气和四种湿度。给出在每种天气下四种湿度的概率,和天气之间的转移概率,如今给出一个n天的湿度序列,要求给出一个概率最大的天气序列。

思路:非常easy的概率DP。转移概率和状态概率都给好了。设dp[i][j]表示第i天是第j种天气的最大概率。然后pre[i][j]来记录序列顺序。

如果第i-1天是第j1种天气。第i天是j2种天气。天气转移概率为p1[j1][j2],第i天的湿度为x,在第j2种天气下x湿度的概率为p2[j2][x],则有:

dp[i][j2]=max(dp[i-1][j1]*p1[j1][j2]*p2[j2][x]),记录最大值是由哪一个j1转移过来的,用pre[i][j2]记录。最后求得最大的dp[n][j]。依据pre数组输出路径就可以。

注:题目中说因为dp[n][j]可能非常小。用double乘可能会掉精度,所以要用log,可是我直接乘也过了,可能是运气比較好吧,以后这样的问题还是要多注意。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <vector>
#define maxn 55
using namespace std;
double dp[55][3];
int pre[55][3];
map<string,int> mp;
string str;
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};
void init()
{
mp.insert(make_pair("Dry",0));
mp.insert(make_pair("Dryish",1));
mp.insert(make_pair("Damp",2));
mp.insert(make_pair("Soggy",3));
}
int main()
{
int ncase,T=0;
scanf("%d",&ncase);
init();
while(ncase--)
{
printf("Case #%d:\n",++T);
int n;
scanf("%d",&n);
cin>>str;
for(int i=0;i<=n;i++)
{
for(int j=0;j<3;j++)
dp[i][j]=0;
}
int lab=mp[str];
memset(pre,0,sizeof(pre));
dp[1][0]=0.63*p1[0][lab];
dp[1][1]=0.17*p1[1][lab];
dp[1][2]=0.2*p1[2][lab];
for(int i=2;i<=n;i++)
{
cin>>str;
int lab=mp[str];
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
double pp=dp[i-1][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=0;
int po;
for(int i=0;i<3;i++)
{
if(dp[n][i]>mi)
{
mi=dp[n][i];
po=i;
}
}
ans.push_back(po);
int now=n;
while(now!=1)
{
po=pre[now][po];
ans.push_back(po);
now--;
}
for(int i=n-1;i>=0;i--)
{
if(ans[i]==0)
printf("Sunny\n");
else if(ans[i]==1)
printf("Cloudy\n");
else
printf("Rainy\n");
}
}
return 0;
}


hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)的更多相关文章

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

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

  2. hdu 4869 Turn the pokers (2014多校联合第一场 I)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu 4865 Peter&#39;s Hobby

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

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

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

  5. 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 ...

  6. HDU 4870 Rating (2014 多校联合第一场 J)(概率)

    题意: 一个人有两个TC的账号,一开始两个账号rating都是0,然后每次它会选择里面rating较小的一个账号去打比赛,每次比赛有p的概率+1分,有1-p的概率-2分,当然如果本身是<=2分的 ...

  7. HDU 4869 Turn the pokers (2014 多校联合第一场 I)

    HDOJ--4869--Turn the pokers[组合数学+快速幂] 题意:有m张扑克,开始时全部正面朝下,你可以翻n次牌,每次可以翻xi张,翻拍规则就是正面朝下变背面朝下,反之亦然,问经过n次 ...

  8. HDU 4868 Information Extraction(2014 多校联合第一场 H)

    看到这道题时我的内心是奔溃的,没有了解过HTML,只能靠窝的渣渣英语一点一点翻译啊TT. Information Extraction 题意:(纯手工翻译,有些用词可能在html中不是一样的,还多包涵 ...

  9. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

随机推荐

  1. 截取字符(substr)检索字符位置(instr)

    1.SUBSTR(string,start_position,[length]) 求子字符串,返回字符串注释: string 元字符串start_position 开始位置(从0开始)length 可 ...

  2. C# Event.ClickCount 解决垃圾鼠标带来的烦恼

    今天调试遇到个Bug,百思不得其解的是在自己的设备上重来不重现,在测试机上百分百重现,如下: 问题:点击一次Button执行两次Click操作 分析:看Log的确是执行了两次,就像真的点击了两次But ...

  3. thinkphp的ip地址定位

    在WEB应用中,根据IP地址定位和记录相关访问日志也是非常常见的需求,在ThinkPHP中你可以轻松的实现IP地址获取和定位. 获取扩展类库 可以在官网的http://www.thinkphp.cn/ ...

  4. 通过JS制作一个简易数码时钟

    设计思路: 数码时钟即通过图片数字来显示当前时间,需要显示的图片的URL根据时间变化而变化. a.获取当前时间Date()并将当前时间信息转换为一个6位的字符串; b.根据时间字符串每个位置对应的数字 ...

  5. python 实现线程之间的通信

    前言:因为GIL的限制,python的线程是无法真正意义上并行的.相对于异步编程,其性能可以说不是一个等量级的.为什么我们还要学习多线程编程呢,虽然说异步编程好处多,但编程也较为复杂,逻辑不容易理解, ...

  6. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree2

    试了一下先上再下的Treap方式,很高兴,代码变短了,但是,跑的变慢了!!!其实慢得不多,5%左右.而且这个版本的写法不容易写错..只要会一般可持久化Treap的人写着都不难...就是相对于(压行的) ...

  7. 自学Python十 爬虫实战三(美女福利续)

    我又来送福利啦!!!不同于上篇文章,这次我们的爬虫采用了多线程,一直以来被所谓的分布式  多线程  爬虫 给唬的怕怕的.今天就来一发多线程爬虫吧,还能看妹子图,想想就觉得很激动!!! 依然是流程解释: ...

  8. C#中通过js实现个人用户和非个人用户的登陆

    实现用户的登录功能,这里举一个个人和非个人用户的登录的例子 前台代码: <ul class="login_list clearfix"> <li> < ...

  9. C#使用各种时间戳及转换

    /// <summary> /// DateTime时间格式转换为13位带毫秒的Unix时间戳 /// </summary> /// <param name=" ...

  10. JavaScript图片轮播,举一反三

    图片轮播,在一些购物网站上运用的不胜枚举,下面简单介绍一下图片轮播的实现. 如图 <!doctype html> <html lang="en"> < ...