Peter's Hobby
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 500 Accepted Submission(s): 215
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 RainyHintLog is useful.
Author
FZU
Source
概率DP。。。
设第i天看到的叶子为a[i]。
dp[i][j]=第i天,天气为j时看到a[i]的概率。
pre[i][j]: 第i天,天气为j时,前一天天气最可能是什么。。
dp[i][j]=max(dp[i-1][k]*weather[k][j]*leaf[j][a[i]])
为了保证精度,可以对概率取对数。。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
typedef long long ll;
//#pragma comment(linker, "/STACK:1024000000,1024000000") //手动扩栈
#define INF 1e9
#define maxn 500
#define maxm 100086+10
#define mod 1000000007
#define eps 1e-7
#define PI acos(-1.0)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
double wh[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}};
double ww[3][3]={{0.5, 0.375, 0.125},
{0.25, 0.125, 0.625},
{0.25, 0.375, 0.375}};
double st[3] = {0.63,0.17,0.3};
vector<int> ans;
int a[55];
int pre[55][5];
double dp[55][5];
void Pre()
{
rep(i,3) rep(j,4) wh[i][j] = log(wh[i][j]);
rep(i,3) rep(j,3) ww[i][j] = log(ww[i][j]);
rep(i,3) st[i] = log(st[i]);
}
void init()
{
ans.clear();
ini(pre);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
Pre();
int T;
cin>>T;
int cas = 1;
int n;
char s[10];
while(T--)
{
cin>>n;
for(int i=0;i<=n;i++) rep(j,3) dp[i][j] = -INF;
init();
rep1(i,n)
{
scans(s);
if(strcmp(s,"Dry")==0) a[i] = 0;
if(strcmp(s,"Dryish")==0) a[i] = 1;
if(strcmp(s,"Damp")==0) a[i] = 2;
if(strcmp(s,"Soggy")==0) a[i] = 3;
}
rep(i,3) dp[1][i] = st[i] + wh[i][a[1]];
int ma = 0;
for(int i = 2;i <= n; i++)
{
rep(j,3)
{
rep(k,3)
{
if(dp[i-1][k] + ww[k][j] + wh[j][a[i]] > dp[i][j])
{
dp[i][j] = dp[i-1][k] + ww[k][j] + wh[j][a[i]];
ma = k;
}
}
pre[i][j] = ma;
}
}
double mx = -INF;
rep(i,3)
{
if(dp[n][i] > mx)
{
mx = dp[n][i];
ma = i;
}
}
ans.push_back(ma);
for(int i=n;i>=2;i--)
{
ans.push_back(pre[i][ma]);
ma = pre[i][ma];
}
printf("Case #%d:\n",cas++);
for(int i=n-1;i>=0;i--)
{
if(ans[i] == 0) puts("Sunny");
else if(ans[i] == 1) puts("Cloudy");
else puts("Rainy");
}
}
return 0;
}