设为首页 加入收藏

TOP

hdu 4865 Peter's Hobby (隐马尔可夫模型 dp)(一)
2015-07-20 18:04:07 来源: 作者: 【 】 浏览:9
Tags:hdu 4865 Peter' Hobby 马尔 可夫 模型

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 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 mZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3QgcHJvYmFibHkgbGlrZSBpbiBvcmRlcj8KIAo8YnI+CklucHV0ClRoZSBmaXJzdCBsaW5lIGlzIFQsIG1lYW5zIHRoZSBudW1iZXIgb2YgY2FzZXMsIHRoZW4gdGhlIGZvbGxvd2luZ3MgYXJlIFQgY2FzZXMuIGZvciBlYWNoIGNhc2U6PGJyPgpUaGUgZmlyc3QgbGluZSBpcyBhIGludGVnZXIgbihuPD01MCksbWVhbnMgdGhlIG51bWJlciBvZiBkYXlzLCBhbmQgdGhlIG5leHQgbiBsaW5lcywgZWFjaCBsaW5lIGlzIGEgc3RyaW5nIHNob3dzIHRoZSBodW1pZGl0eSBvZiBsZWF2ZXMgKERyeSwgRHJ5aXNoLCBEYW1wLCBTb2dneSkKIAo8YnI+Ck91dHB1dApGb3IgZWFjaCB0ZXN0IGNhc2UsIHByaW50IHRoZSBjYXNlIG51bWJlciBvbiBpdHMgb3duIGxpbmUuIFRoZW4gaXMgdGhlIG1vc3QgcG9zc2libGUgd2VhdGhlciBzZXF1ZW5jZS4oIFdlIGd1YXJhbnRlZSB0aGF0IHRoZSBkYXRhIGhhcyBhIHVuaXF1ZSBzb2x1dGlvbikKIAo8YnI+ClNhbXBsZSBJbnB1dAoKPHByZSBjbGFzcz0="brush:java;">1 3 Dry Damp Soggy
Sample Output
Case #1:
Sunny
Cloudy
Rainy

HintLog is useful. 

题意: 真难解释清楚,给你一个海藻的状态序列,让你找出一个最有可能的天气序列,前一天的天气能影响今天的天气,海藻的状态受天气的影响。(从题目中应该是读不出他是怎样影响的,具体看下面的推荐的链接)
思路: 这其实是隐马尔可夫模型(HMM)的一个应用,根据可观察状态的序列找到一个最可能的隐藏状态序列,隐马尔可夫模型介绍见这里:点击打开链接 dp[i][j]表示第i天天气为j的概率,dp[i][j]=dp[i-1][k]*wea[k][j]*lea[j][num[i]],记录路径,最后输出最有可能的路径,根据最后一天的概率来看。
代码1:(不用log的代码)
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #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
             
              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; } 
             
            
           
          
         
       
      
     
    
   
  


代码2:(乘法多了之后损失精度,可以用log)
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #includ
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU4734――F(x)(数位DP) 下一篇poj 2054 Color a Tree(贪心)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: