Peter's Hobby

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

Total Submission(s): 292    Accepted Submission(s): 132
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.
 

题意:
真难解释清楚。给你一个海藻的状态序列,让你找出一个最有可能的天气序列。前一天的天气能影响今天的天气,海藻的状态受天气的影响。

(从题目中应该是读不出他是如何影响的。详细看以下的推荐的链接)


思路:
这事实上是隐马尔可夫模型(HMM)的一个应用,依据可观察状态的序列找到一个最可能的隐藏状态序列,隐马尔可夫模型介绍见这里:点击打开链接
dp[i][j]表示第i天天气为j的概率。dp[i][j]=dp[i-1][k]*wea[k][j]*lea[j][num[i]],记录路径,最后输出最有可能的路径。依据最后一天的概率来看。


代码1:(不用log的代码)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define MAXN 100005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-10
typedef long long ll;
using namespace std; int n,m,flag,cnt,tot,test=0;
int num[55],pre[55][4];
double dp[55][4];
char s[50];
double lea[3][4]=
{
{0.6, 0.2, 0.15, 0.05},
{0.25, 0.3, 0.2, 0.25},
{0.05, 0.10, 0.35, 0.50}
};
double wea[3][3]=
{
{0.5, 0.375, 0.125},
{0.25, 0.125, 0.625},
{0.25, 0.375, 0.375}
};
char res[3][15]=
{
"Sunny","Cloudy","Rainy"
}; void output(int x,int y)
{
if(x==0) return ;
output(x-1,pre[x][y]);
printf("%s\n",res[y]);
}
void solve()
{
int i,j,k,t,id;
double ma,tmp;
dp[1][0]=0.63*lea[0][num[1]];
dp[1][1]=0.17*lea[1][num[1]];
dp[1][2]=0.2*lea[2][num[1]];
for(i=2;i<=n;i++)
{
for(j=0;j<3;j++)
{
ma=-1;
for(k=0;k<3;k++)
{
tmp=dp[i-1][k]*wea[k][j]*lea[j][num[i]];
if(ma<tmp)
{
ma=tmp; id=k;
}
}
dp[i][j]=ma; pre[i][j]=id;
}
}
ma=-1;
for(j=0;j<3;j++)
{
if(dp[n][j]>ma)
{
ma=dp[n][j]; id=j;
}
}
printf("Case #%d:\n",++test);
output(n,id);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(strcmp(s,"Dry")==0) num[i]=0;
else if(strcmp(s,"Dryish")==0) num[i]=1;
else if(strcmp(s,"Damp")==0) num[i]=2;
else num[i]=3;
}
solve();
}
return 0;
}

代码2:(乘法多了之后损失精度,能够用log)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define MAXN 100005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-10
typedef long long ll;
using namespace std; int n,m,flag,cnt,tot,test=0;
int num[55],pre[55][4];
double dp[55][4];
char s[50];
double lea[3][4]=
{
{0.6, 0.2, 0.15, 0.05},
{0.25, 0.3, 0.2, 0.25},
{0.05, 0.10, 0.35, 0.50}
};
double wea[3][3]=
{
{0.5, 0.375, 0.125},
{0.25, 0.125, 0.625},
{0.25, 0.375, 0.375}
};
char res[3][15]=
{
"Sunny","Cloudy","Rainy"
}; void output(int x,int y)
{
if(x==0) return ;
output(x-1,pre[x][y]);
printf("%s\n",res[y]);
}
void solve()
{
int i,j,k,t,id;
double ma,tmp;
dp[1][0]=log(0.63)+lea[0][num[1]];
dp[1][1]=log(0.17)+lea[1][num[1]];
dp[1][2]=log(0.2)+lea[2][num[1]];
for(i=2;i<=n;i++)
{
for(j=0;j<3;j++)
{
ma=-INF;
for(k=0;k<3;k++)
{
tmp=dp[i-1][k]+wea[k][j]+lea[j][num[i]];
if(ma<tmp)
{
ma=tmp; id=k;
}
}
dp[i][j]=ma; pre[i][j]=id;
}
}
ma=-INF;
for(j=0;j<3;j++)
{
if(dp[n][j]>ma)
{
ma=dp[n][j];
id=j;
}
}
printf("Case #%d:\n",++test);
output(n,id);
}
int main()
{
int i,j,t;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
lea[i][j]=log(lea[i][j]);
if(j<3) wea[i][j]=log(wea[i][j]);
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(strcmp(s,"Dry")==0) num[i]=0;
else if(strcmp(s,"Dryish")==0) num[i]=1;
else if(strcmp(s,"Damp")==0) num[i]=2;
else num[i]=3;
}
solve();
}
return 0;
}




hdu 4865 Peter&#39;s Hobby (隐马尔可夫模型 dp)的更多相关文章

  1. 隐马尔科夫模型HMM学习最佳范例

    谷歌路过这个专门介绍HMM及其相关算法的主页:http://rrurl.cn/vAgKhh 里面图文并茂动感十足,写得通俗易懂,可以说是介绍HMM很好的范例了.一个名为52nlp的博主(google ...

  2. viterbi维特比算法和隐马尔可夫模型(HMM)

    隐马尔可夫模型(HMM) 原文地址:http://www.cnblogs.com/jacklu/p/7753471.html 本文结合了王晓刚老师的ENGG 5202 Pattern Recognit ...

  3. 隐马尔科夫模型python实现简单拼音输入法

    在网上看到一篇关于隐马尔科夫模型的介绍,觉得简直不能再神奇,又在网上找到大神的一篇关于如何用隐马尔可夫模型实现中文拼音输入的博客,无奈大神没给可以运行的代码,只能纯手动网上找到了结巴分词的词库,根据此 ...

  4. 一文搞懂HMM(隐马尔可夫模型)

    什么是熵(Entropy) 简单来说,熵是表示物质系统状态的一种度量,用它老表征系统的无序程度.熵越大,系统越无序,意味着系统结构和运动的不确定和无规则:反之,,熵越小,系统越有序,意味着具有确定和有 ...

  5. HMM基本原理及其实现(隐马尔科夫模型)

    HMM(隐马尔科夫模型)基本原理及其实现 HMM基本原理 Markov链:如果一个过程的“将来”仅依赖“现在”而不依赖“过去”,则此过程具有马尔可夫性,或称此过程为马尔可夫过程.马尔可夫链是时间和状态 ...

  6. 转:隐马尔可夫模型(HMM)攻略

    隐马尔可夫模型 (Hidden Markov Model,HMM) 最初由 L. E. Baum 和其它一些学者发表在一系列的统计学论文中,随后在语言识别,自然语言处理以及生物信息等领域体现了很大的价 ...

  7. [综]隐马尔可夫模型Hidden Markov Model (HMM)

    http://www.zhihu.com/question/20962240 Yang Eninala杜克大学 生物化学博士 线性代数 收录于 编辑推荐 •2216 人赞同 ×××××11月22日已更 ...

  8. 隐马尔可夫模型(Hidden Markov Model,HMM)

    介绍 崔晓源 翻译 我们通常都习惯寻找一个事物在一段时间里的变化规律.在很多领域我们都希望找到这个规律,比如计算机中的指令顺序,句子中的词顺序和语音中的词顺序等等.一个最适用的例子就是天气的预测. 首 ...

  9. 基于隐马尔科夫模型(HMM)的地图匹配(Map-Matching)算法

    文章目录 1. 1. 摘要 2. 2. Map-Matching(MM)问题 3. 3. 隐马尔科夫模型(HMM) 3.1. 3.1. HMM简述 3.2. 3.2. 基于HMM的Map-Matchi ...

随机推荐

  1. MFC 获得各类指针、句柄的方法(转)

    原文转自 https://blog.csdn.net/abcjennifer/article/details/7480019 1.MFC中获取常见类句柄<视图类,文档类,框架类,应用程序类> ...

  2. (转)python爬虫----(scrapy框架提高(1),自定义Request爬取)

    摘要 之前一直使用默认的parse入口,以及SgmlLinkExtractor自动抓取url.但是一般使用的时候都是需要自己写具体的url抓取函数的. python 爬虫 scrapy scrapy提 ...

  3. 给Input type='date'赋值

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) 需要使用AngularJS动态给<input type="date" />赋值. 我使用的是ng-bind=&qu ...

  4. Android应用开发之使用Socket进行大文件断点上传续传

    http://www.linuxidc.com/Linux/2012-03/55567.htm http://blog.csdn.net/shimiso/article/details/8529633 ...

  5. 牛客网 牛客练习赛7 B.购物-STL(priority_queue)

    B.购物 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 在遥远的东方,有一家糖果专卖店 这家糖果 ...

  6. python 将windows字体中的汉字生成图片的方法

    #encoding: utf-8import osimport pygame chinese_dir = '黑体常规'if not os.path.exists(chinese_dir): os.mk ...

  7. Codeforces 825E - Minimal Labels

    825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...

  8. 设计模式之不变模式(Immutable Pattern)分析

    http://www.iteye.com/topic/959751 最近老有人问我不变模式,我其实也理解得不深,于是花了一些时间进行学习总结,分析了一下不变模式(immutable pattern), ...

  9. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

  10. Java面向对象内测

    功能要求 开发基于控制台的试题信息管理系统.具体要求如下: (1)显示试题信息管理系统主菜单,包括: 1)列出所有试题信息 2)按科目查询 3)按题干查询 4)添加试题 5)删除试题 6)退出系统 p ...