HDU 4865 Peter's Hoby (probability, DP, log)

Source: Internet
Author: User
Tags strcmp

Two influence matrices are provided. One is the influence of the weather on the humidity of the day, and the other is the influence of the weather of the day before.

That is, the probability that dry (dryish, damp, soggy) occurs on a sunny day (cloudy or rainy day), and the probability that a sunny day (cloudy or rainy day) occurs on a previous day (cloudy or rainy day.

The probability of a cloudy day on the first day is 0.63, 0.17, and 0.20.

Enter the humidity of N days to output the most likely weather of N days.

 

Use DP [I] [J] to represent the probability that day I is J, and use pre [I] [J] to represent its precursor.

Note that log amplification is required because the probability is multiplied too many times .. Otherwise it will be close to 0, with insufficient precision and large error

DP [I] [J] = max {DP [I-1] [k] + w_w [k] [J] + w_h [J] [H [I]}, of course, all of these w_w and w_h must be logged.

W_w [k] [J] indicates the probability of yesterday's weather today J. w_h [J] [H [I] indicates the probability of today's weather occurrence H [I] humidity.

In this way, DP [I] [J] indicates that day I is J weather and the humidity is H [I].

 

 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6  7 double aa[3][4]={ 8     0.6, 0.2, 0.15, 0.05, 9     0.25, 0.3, 0.2, 0.25,10     0.05, 0.10, 0.35, 0.5011 };12 double bb[3][3]={13     0.5, 0.375, 0.125,14     0.25, 0.125, 0.625,15     0.25, 0.375, 0.37516 };17  int main(){18      for(int i=0;i<3;++i)for(int j=0;j<4;++j) aa[i][j] = log(aa[i][j]);19      for(int i=0;i<3;++i)for(int j=0;j<3;++j) bb[i][j] = log(bb[i][j]);20      int t,n,lea[55],ca=0;21      double dp[55][3];22      int pre[55][3];23      int ans[55];24      scanf("%d",&t);25      while(t--){26          scanf("%d",&n);27          for(int i=0;i<n;++i){28             char s[22];29              scanf("%s",s);30             if(strcmp(s,"Dry")==0) lea[i]=0;31             else if(strcmp(s,"Dryish")==0) lea[i]=1;32             else if(strcmp(s,"Damp")==0) lea[i]=2;33             else lea[i]=3;34          }35          dp[0][0] = log(0.63)+aa[0][lea[0]];36          dp[0][1] = log(0.17)+aa[1][lea[0]];37          dp[0][2] = log(0.20)+aa[2][lea[0]];38          for(int i=1;i<n;++i){39              for(int j=0;j<3;++j){40                 double ma = -1e8; int idx;41                  for(int k=0;k<3;++k){42                      if(dp[i-1][k]+bb[k][j]+aa[j][lea[i]] >ma){43                          ma = dp[i-1][k]+bb[k][j]+aa[j][lea[i]];44                          idx = k;45                      }46                  }47                  dp[i][j] = ma, pre[i][j]=idx;48              }49          }50          double ma=-1e8;int idx;51          for(int j=0;j<3;++j)if(dp[n-1][j]>ma){ma=dp[n-1][j];idx=j;}52          ans[n-1]=idx;53          for(int i=n-1;i;--i){54              ans[i-1] = pre[i][idx];55              idx = pre[i][idx];56          }57          printf("Case #%d:\n",++ca);58          for(int i=0;i<n;++i){59              if(ans[i]==0)puts("Sunny");60              else if(ans[i]==1)puts("Cloudy");61              else puts("Rainy");62          }63      }64      return 0;65  }
View code

 

 

Hidden Markov ModelHttp://blog.csdn.net/likelet/article/details/7056068

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.