hdu 4865 Peter's Hobby (隐马尔可夫模型 dp)
Peter's Hobby
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?
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)
1
3
Dry
Damp
Soggy
Case #1:
Sunny
Cloudy
RainyHintLog is useful.
(从题目中应该是读不出他是如何影响的。详细看以下的推荐的链接)
#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;
}
#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's Hobby (隐马尔可夫模型 dp)的更多相关文章
- 隐马尔科夫模型HMM学习最佳范例
谷歌路过这个专门介绍HMM及其相关算法的主页:http://rrurl.cn/vAgKhh 里面图文并茂动感十足,写得通俗易懂,可以说是介绍HMM很好的范例了.一个名为52nlp的博主(google ...
- viterbi维特比算法和隐马尔可夫模型(HMM)
隐马尔可夫模型(HMM) 原文地址:http://www.cnblogs.com/jacklu/p/7753471.html 本文结合了王晓刚老师的ENGG 5202 Pattern Recognit ...
- 隐马尔科夫模型python实现简单拼音输入法
在网上看到一篇关于隐马尔科夫模型的介绍,觉得简直不能再神奇,又在网上找到大神的一篇关于如何用隐马尔可夫模型实现中文拼音输入的博客,无奈大神没给可以运行的代码,只能纯手动网上找到了结巴分词的词库,根据此 ...
- 一文搞懂HMM(隐马尔可夫模型)
什么是熵(Entropy) 简单来说,熵是表示物质系统状态的一种度量,用它老表征系统的无序程度.熵越大,系统越无序,意味着系统结构和运动的不确定和无规则:反之,,熵越小,系统越有序,意味着具有确定和有 ...
- HMM基本原理及其实现(隐马尔科夫模型)
HMM(隐马尔科夫模型)基本原理及其实现 HMM基本原理 Markov链:如果一个过程的“将来”仅依赖“现在”而不依赖“过去”,则此过程具有马尔可夫性,或称此过程为马尔可夫过程.马尔可夫链是时间和状态 ...
- 转:隐马尔可夫模型(HMM)攻略
隐马尔可夫模型 (Hidden Markov Model,HMM) 最初由 L. E. Baum 和其它一些学者发表在一系列的统计学论文中,随后在语言识别,自然语言处理以及生物信息等领域体现了很大的价 ...
- [综]隐马尔可夫模型Hidden Markov Model (HMM)
http://www.zhihu.com/question/20962240 Yang Eninala杜克大学 生物化学博士 线性代数 收录于 编辑推荐 •2216 人赞同 ×××××11月22日已更 ...
- 隐马尔可夫模型(Hidden Markov Model,HMM)
介绍 崔晓源 翻译 我们通常都习惯寻找一个事物在一段时间里的变化规律.在很多领域我们都希望找到这个规律,比如计算机中的指令顺序,句子中的词顺序和语音中的词顺序等等.一个最适用的例子就是天气的预测. 首 ...
- 基于隐马尔科夫模型(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 ...
随机推荐
- 汕头市队赛 SRM 08 A
比赛没参加 所以回来补题咯 A还是自己YY出来了的 可惜比赛没有打 描述 给一个 01 串设为其 S,询问是否存在只出现两次的 01 串 T. 这里的出现定义为存在一串下标 ,满足 且 . 输入格式 ...
- javascript jquery document.ready window.onload
网易 博客 下载LOFTER客户端 注册登录 加关注 凡图的编程之路 2012年7月从一个编程新手的点点滴滴 首页 日志 LOFTER 相册 博友 关于我 日志 关于我 Holy ...
- BEE网站
http://www.bee-framework.com/ http://syxiaqj.github.io/2014/02/28/bee-learning-1/#0-tsina-1-24637-39 ...
- read 系统调用剖析【转】
转自:https://www.ibm.com/developerworks/cn/linux/l-cn-read/ 大部分程序员可能会有这样的疑问:当在程序中调用库函数 read 时,这个请求是经过哪 ...
- java使用BeanUtils封装file类型表单数据到一个对象中
package com.cc.web.servlet; import java.io.FileOutputStream; import java.io.IOException; import java ...
- hdu 3264(枚举+二分+圆的公共面积)
Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- array数据初始化
#include <iostream> int main() { ]={}; std::cout<<array[]<<]; } 试了试上面的代码发现,数组在用{}赋 ...
- ZOJ 3937 More Health Points (2016 浙江省赛 B题,可持久维护凸壳)
题目链接 2016 ZJCPC Problem B 题意 CF 660F的树上版本. 其他做的方法都差不多,关键是把凸壳放到树上. 每次确定扔掉几个元素的时候直接$O(1)$修改(先不清楚这个位置 ...
- 信息批量提取工具bulk-extractor
信息批量提取工具bulk-extractor 在数字取证中,通常需要面对海量的数据,如几百GB甚至TB级别的数据.从这些海量数据中,提取有价值的数据是一个漫长.枯燥.繁琐的过程.Kali Linu ...
- Burnside引理与polay定理
#Burnside引理与polay定理 引入概念 1.置换 简单来说就是最元素进行重排列 是所有元素的异议映射,即\([1,n]\)映射到\([1,n]\) \[ \begin{pmatrix} 1& ...