首页 > 代码库 > Peter's Hobby
Peter's Hobby
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?
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)
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.概率dp time :0ms
#include"iostream"#include"cstdio"#include"map"#include"cstring"#include"string"#include"algorithm"#include"vector"using namespace std;const int ms=51;double p1[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 p2[3][3]= {0.5,0.375,0.125,0.25,0.125,0.625,0.25,0.375,0.375};double dp[ms][3];int pre[ms][3];map<int,string> m1;map<string,int> m2;string str;void init(){ m2.insert(make_pair("Dry",0)); m2.insert(make_pair("Dryish",1)); m2.insert(make_pair("Damp",2)); m2.insert(make_pair("Soggy",3)); m1.insert(make_pair(0,"Sunny")); m1.insert(make_pair(1,"Cloudy")); m1.insert(make_pair(2,"Rainy"));}int main(){ int ncase,T=0; scanf("%d",&ncase); init(); while(ncase--) { printf("Case #%d:\n",++T); int n,i,j,k; scanf("%d",&n); cin>>str; for(i=0;i<=n;i++) for(j=0;j<3;j++) dp[i][j]=0; int lab=m2[str]; memset(pre,0,sizeof(pre)); dp[1][0]=0.63*p1[0][lab]; dp[1][1]=0.17*p1[1][lab]; dp[1][2]=0.2*p1[2][lab]; for(i=2;i<=n;i++) { cin>>str; lab=m2[str]; for(j=0;j<3;j++) for(k=0;k<3;k++) { double pp=dp[i-1][k]*p2[k][j]*p1[j][lab]; if(pp>dp[i][j]) { dp[i][j]=pp; pre[i][j]=k; } } } vector<int> ans; double mi=0; int po; for(i=0;i<3;i++) if(dp[n][i]>mi) { mi=dp[n][i]; po=i; } ans.push_back(po); int now=n; while(now!=1) { po=pre[now][po]; ans.push_back(po); now--; } for(i=n-1;i>=0;i--) { //printf("%s\n",m1[ans[i]]); cout<<m1[ans[i]]<<endl; } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。