2014 Multi-University Training Contest 1 - 1005(Peter's Hobby)

解决一道算法题目,通过概率矩阵预测连续几天最可能的天气情况来匹配已知的叶子湿度记录。

http://acm.hdu.edu.cn/showproblem.php?pid=4865

 

Peter's Hobby

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


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

 

 

以下摘自 http://www.cnblogs.com/nextbin/p/3864234.html  . 感谢大牛的解释..

给出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][j] = max{dp[i-1][k] + w_w[k][j] + w_h[j][h[i]]},当然这些w_w, w_h要全部都log

w_w[k][j]表示昨天k天气今天j天气的概率,w_h[j][h[i]]表示今天j天气发生h[i]湿度的概率

这样出来的dp[i][j]就可以表示第i天为j天气,且湿度为h[i] 。

 

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <numeric>
#include <bitset>

#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define rof(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define pb push_back


////////////////////////////////////////////////////////////////////////////////
int T,n;
char ch[20];
long double f[60][3];
long double a[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
};
long double b[3][3]={
    0.5,0.375,0.125,
    0.25,0.125,0.625,
    0.25,0.375,0.375
};
int pre[60][3];
int leaf[60];


int main()
{
    //freopen("in.txt","r",stdin);
    ios_base::sync_with_stdio(0);
    scanf("%d",&T);
    rep(i,3) rep(j,4) a[i][j]=log(a[i][j]);
    rep(i,3) rep(j,3) b[i][j]=log(b[i][j]);

    fer(tt,1,T+1){
        printf("Case #%d:\n", tt);
        scanf("%d",&n);
        rep(i,n){
            scanf("\n%s",ch);
            switch(ch[3]){
                case 'i':leaf[i]=1; break;
                case 'p':leaf[i]=2; break;
                case 'g':leaf[i]=3; break;
                default: leaf[i]=0; break;
            }
        }
        
        f[0][0]=log(0.63)+a[0][leaf[0]]; f[0][1]=log(0.17)+a[1][leaf[0]]; f[0][2]=log(0.2)+a[2][leaf[0]];
        fer(i,1,n){
            rep(j,3){
                long double ma = -10000000,tmp; int mk;
                rep(k,3){
                    tmp = f[i-1][k]+b[k][j]+a[j][leaf[i]];
                    if(tmp>ma) ma=tmp,mk=k;
                }
                f[i][j]=ma; pre[i][j]=mk;
            }
        }

        long double ma = -10000000; int mk;
        rep(k,3) if(f[n-1][k]>ma) ma=f[n-1][k],mk=k;

        std::vector<int> v;
        rof(i,n,0){
            v.pb(mk); mk=pre[i][mk];
        }
        rof(i,n,0){
            if(v[i]==0) puts("Sunny");
            else if(v[i]==1) puts("Cloudy");
            else puts("Rainy");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/rewrite/p/3942220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值