HDU 4865 Peter's Hobby (HMM模型,维特比算法,概率dp)

Peter's Hobby

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1051    Accepted Submission(s): 469

 

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

Source

2014 Multi-University Training Contest 1

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4865

题目大意:已知第一天是某类天气的概率,天气概率的转移矩阵以及天气对应叶子湿度的概率矩阵,现给出n天叶子分别的湿度情况,求这n天可能性最大的天气情况序列

题目分析:HMM模型,采用维比特算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <map>
#include <cmath>
using namespace std;

string s;
double start_pr[3] = {0.63, 0.17, 0.2};
double transition_pr[3][3] = {
	0.5, 0.375, 0.125,
	0.25, 0.125, 0.625,
	0.25, 0.375, 0.375
};
double emission_pr[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
};
map <string, int> mp;
map <int, string> rmp;
double dp[55][5];
int path[55][5], ans[55];

void init() {
	mp["Dry"] = 0; mp["Dryish"] = 1; mp["Damp"] = 2; mp["Soggy"] = 3;
	rmp[0] = "Sunny"; rmp[1] = "Cloudy"; rmp[2] = "Rainy";
}

int main() {
	init();
	int T, n;
	scanf("%d", &T);
	for (int ca = 1; ca <= T; ca++) {
		scanf("%d", &n);
		memset(dp, 0, sizeof(dp));
		cin >> s;
		for (int j = 0; j < 3; j++) {
			dp[1][j] = start_pr[j] * emission_pr[j][mp[s]];
		}
		for (int i = 2; i <= n; i++) {
			cin >> s;
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k < 3; k++) {
					double p = dp[i - 1][k] * transition_pr[k][j] * emission_pr[j][mp[s]];
					if (dp[i][j] < p) {
						dp[i][j] = p;
						path[i][j] = k;
					}
				}
			}
		}
		int sta = 0;
		for (int j = 1; j < 3; j++) {
			if (dp[n][j] > dp[n][sta]) {
				sta = j;
			}
		}
		for (int i = n; i > 0; i--) {
			ans[i] = sta;
			sta = path[i][sta];
		}
		printf("Case #%d:\n", ca);
		for (int i = 1; i <= n; i++) {
			cout << rmp[ans[i]] << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Tc_To_Top/article/details/81665273