HDU 4865 Peter's Hobby

Peter‘s Hobby

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

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.

Author

FZU

Source

2014 Multi-University Training Contest 1

Recommend

We have carefully selected several similar problems for you:  4959 4958 4957 4956 4955

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#define REP(a,b,c) for(a=b;a<=c;a++)
using namespace std;
typedef long long ll;
const ll inf= 1e8;
const int N=52;
const int M=16 ;
double dp[N][5];

double p[3][3]={{0.5,0.375,0.125},
                {0.25,0.125,0.625},
                {0.25,0.375,0.375}
                };
double q[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}
                };

int n;
int hash(string str)
{
    if(str=="Dry")return 0;
    else if(str=="Dryish")return 1;
    else if(str=="Damp")return 2;
    else return 3;
}

int x[N],pa[N][3],ans[N];
char * weather[3]={(char*)"Sunny",(char*)"Cloudy",(char*)"Rainy"};
int run()
{
    string str;
    int _,cas=0;
    cin>>_;
    while(_--)
    {
        cin>>n;
        for(int i=1;i<=n;++i){
            cin>>str;
            x[i] = hash(str);
        }
        for(int i=1;i<=n;++i){
            for(int j=0;j<3;++j)
                dp[i][j]= log(0);
        }
        memset(pa,0,sizeof(pa));
        dp[1][0] = log(0.63) + log(q[0][x[1]]);
        dp[1][1] = log(0.17) + log(q[1][x[1]]);
        dp[1][2] = log(0.20) + log(q[2][x[1]]);
        for(int i=2;i<=n;++i)
            for(int j=0;j<3;++j)
                for(int k=0;k<3;++k){
                    double temp=dp[i-1][k]+log(p[k][j])+log(q[j][x[i]]);
                    if(temp > dp[i][j] )
                    {
                        dp[i][j]=temp;
                        pa[i][j]=k;
                    }
                }
        int pos=0;
        for(int i=0;i<3;++i)
            if(dp[n][i] > dp[n][pos]) pos=i;
        ans[n]=pos;
        for(int i=n-1;i>=1;--i){
            pos=pa[i+1][pos];
            ans[i]=pos;
        }
            printf("Case #%d:\n",++cas);
        for(int i=1;i<=n;++i)
            printf("%s\n",weather[ans[i]]);

    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(0);
    return run();
}

HDU 4865 Peter's Hobby,布布扣,bubuko.com

HDU 4865 Peter's Hobby

时间: 2024-08-02 02:48:19

HDU 4865 Peter's Hobby的相关文章

HDU 4865 Peter&#39;s Hobby --概率DP

题意:第i天的天气会一定概率地影响第i+1天的天气,也会一定概率地影响这一天的湿度.概率在表中给出.给出n天的湿度,推测概率最大的这n天的天气. 分析:这是引自机器学习中隐马尔科夫模型的入门模型,其实在这里直接DP就可以了 定义:dp[i][j]为第i天天气为j(0,1,2分别表示三个天气)的概率,path[i][j]记录路径,path[i][j] = k 意思是前一天天气为k时,这一天有最大的概率是天气j. 做一个三重循环,对于每天,枚举今天的天气,再在里面枚举昨天的天气,则有: dp[i][

HDU 4865 Peter&#39;s Hobby(概率、dp、log)

给出2个影响矩阵,一个是当天天气对湿度的影响,一个是前一天天气对当天天气的影响. 即在晴天(阴天.雨天)发生Dry(Dryish.Damp.Soggy)的概率,以及前一天晴天(阴天.雨天)而今天发生晴天(阴天.雨天)的概率. 其中第一天的晴天阴天雨天概率为0.63,0.17,0.20 输入n天的湿度情况,输出最有可能的n天的天气. 用dp[i][j]表示第i天为j天气的概率,用pre[i][j]表示它的前驱. 注意由于概率相乘次数过多,要用log放大..不然会接近0.精度不够.误差大 dp[i]

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

http://acm.hdu.edu.cn/showproblem.php?pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,分别是每种天气下叶子呈现状态的概率和今天天气对明天天气的概率.给出n天叶子的状态,输出最有可能的天气序列. 思路:wl[i][j]表示天气为i,叶子为j的概率,ww[i][j]表示今天天气为i明天天气为j的概率,st[i]表示第一天天气为i的概率. 对于叶子序列{a1,a2......an},存在一个天气序列{b1,b2......bn},那么总的概率g[

hdu 4865 Peter&#39;s Hobby (隐马尔可夫模型 dp)

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

2014多校第一场 E 题 || HDU 4865 Peter&#39;s Hobby (DP)

题目链接 题意 : 给你两个表格,第一个表格是三种天气下出现四种湿度的可能性.第二个表格是,昨天出现的三种天气下,今天出现三种天气的可能性.然后给你这几天的湿度,告诉你第一天出现三种天气的可能性,让你求出最可能出现的天气序列 . 思路 : 定义第 i 天叶子湿度为hum[i].第 i 天,天气为 j 的最大概率为dp[i][j].wealea[i][j]表示天气为 i 叶子为j的概率,weawea[i][j]表示今天天气为 i 明天天气为j的概率,st[i]表示第一天天气为i的概率.pre[i]

hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2.给定n天的叶子湿度状态,求这n天最可能的天气情况 分析:概率dp设 dp[i][j] 表示第i天天气为j的最大概率,pre[i][j]表示第i天天气最可能为j的前一天天气,dp[i][j]=max(dp[i-1][k]+log(wePro[k][j])+log(lePro[j][lePos[i]]

hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

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

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

http://acm.hdu.edu.cn/showproblem.php? pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的概率. 给出n天叶子的状态.输出最有可能的天气序列. 思路:wl[i][j]表示天气为i,叶子为j的概率,ww[i][j]表示今天天气为i明天天气为j的概率,st[i]表示第一天天气为i的概率. 对于叶子序列{a1,a2......an},存在一个天气序列{b1,b2......bn},那么总的概率

hdu 4865 Peter&amp;#39;s Hobby (隐马尔可夫模型 dp)

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