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 ...
随机推荐
- OpenGL入门学习 课程 (三) 绘制几何图形的一些细节问题
http://oulehui.blog.163.com/blog/static/79614698201191832753312/ 先回顾一下我们都学习了些什么: 第一课,编写第一个OpenGL程序第二 ...
- C++11中的小细节--字符串的原始字面量
原始字面量很容易理解,即不进行转义的完整字符串. 最近看了看Python,其中讲到了原始字符串. Both string and bytes literals may optionally be pr ...
- Hashmap与Hashtable的区别及Hashmap的原理
Hashtable和HashMap有几个主要的不同:线程安全以及速度.仅在你需要完全的线程安全的时候使用Hashtable,而如果你使用Java 5或以上的话,请使用ConcurrentHashMap ...
- 解决svn 异常:svn: E155027: Tree conflict can only be resolved to working state; {0} not resolved
以前很少使用svn进行代码管理,时间长了之后也忘得差不多了,但现在公司使用的是svn进行版本管理,使用过程中出现了问题,顺带记一下. 异常情况:切换svn地址之后,发现项目代码无法合并代码,也无法提交 ...
- RANSAC中迭代次数的计算
假设 $w=\frac{内点个数 }{所有点的个数}$. 则 $p_{0}=w^n$表示采样的$n$个点全为内点的概率(可重复) 则至少有一个为外点的概率$p_{1}=1-p_{0}$ 则重复$K$次 ...
- SSOJ 2316 面积【DFS/Flood Fill】
题目描述 编程计算由“1”号围成的下列图形的面积.面积计算方法是统计1号所围成的闭合曲线中点的数目. 如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15. 题目大意:对于给定的1 ...
- quailty's Contest #1 道路修建 EXT(启发式合并)
题目链接 道路修建 EXT 考虑并查集的启发式合并,合并的时候小的子树的根成为大的子树的根的儿子. 可以证明这样整棵树的深度不会超过$logn$. 两个根合并的时候,产生的新的边的边权为当前的时间. ...
- 将一个txt里的A和B谈话内容获取出来并分别保存到A和B的txt文件中
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream;import java.io.Fi ...
- 洛谷 P4538 收集邮票
题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也很喜欢邮票,所 ...
- php的function() use($args)用法
使用use返回 aaa aaa.使用函数传参数aaa bbb. use的参数必须是已经存在的,如果没有定义返回Notice: Undefined variable: word ,使用函数参数方式不需要 ...