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
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)
以下摘自 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; }