• hdu 4865 Peter's Hobby(概率dp)


    http://acm.hdu.edu.cn/showproblem.php?

    pid=4865


    大致题意:有三种天气和四种叶子状态。给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的概率。

    给出n天叶子的状态。输出最有可能的天气序列。


    思路:wl[i][j]表示天气为i,叶子为j的概率,ww[i][j]表示今天天气为i明天天气为j的概率,st[i]表示第一天天气为i的概率。

    对于叶子序列{a1,a2......an},存在一个天气序列{b1,b2......bn},那么总的概率g[n]=st[b1]*wl[b1][a1]*ww[b1][b2]*wl[b2][a2]*......*ww[bn-1][bn]*wl[bn][an]。即log(g[n])=log(st[b1])+log(wl[b1][a1])+log(ww[b1][b2])+log(wl[b2][a2])+......+log(ww[bn-1][bn])+log(wl[bn][an])。

    求log(g[n])的最大值相应的序列就是天气序列。

    相当于给一个n*3的矩阵。代表有n天,每天有3种天气,从第一行到第n行求出一条最长路,输出路径。


    #include <stdio.h>
    #include <iostream>
    #include <map>
    #include <set>
    #include <stack>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <queue>
    #include <string>
    #include <stdlib.h>
    #include <algorithm>
    #define LL long long
    #define _LL __int64
    #define eps 1e-12
    #define PI acos(-1.0)
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    double wl[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 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.2};
    int n;
    char s[10];
    int a[55];
    double f[55][5];
    double dp[55][5];
    int pre[55][5];
    
    stack <int> sta;
    
    int init(char s[])
    {
    	if(strcmp(s,"Dry") == 0)
    		return 0;
    	if(strcmp(s,"Dryish") == 0)
    		return 1;
    	if(strcmp(s,"Damp") == 0)
    		return 2;
    	if(strcmp(s,"Soggy") == 0)
    		return 3;
    }
    
    void To()
    {
    	for(int i = 0; i < 3; i++)
    	{
    		for(int j = 0; j < 3; j++)
    			ww[i][j] = log(ww[i][j]);
    	}
    	for(int i = 0; i < 3; i++)
    		st[i] = log(st[i]);
    }
    
    int main()
    {
    	To();
    	int test;
    	scanf("%d",&test);
    	for(int item = 1; item <= test; item++)
    	{
    		scanf("%d",&n);
    		memset(pre,-1,sizeof(pre));
    		memset(f,0,sizeof(f));
    		for(int i = 1; i <= n; i++)
    		{
    			scanf("%s",s);
    			a[i] = init(s);
    		}
    
    		for(int i = 1; i <= n; i++)
    		{
    			//对于当前天的叶子状态是固定的。如今的f[i][j]表示第i天在叶子固定的条件下天气为j的概率。
    			//所以f[i][j]为每种天气占总天气的比值
    			double t = 0;
    			for(int j = 0; j < 3; j++)
    			{
    				f[i][j] = wl[j][a[i]];
    				t += wl[j][a[i]];
    			}
    			for(int j = 0; j < 3; j++)
    				f[i][j] /= t;
    		}
    
    		for(int i = 1; i <= n; i++)
    		{
    			for(int j = 0; j < 3; j++)
    				f[i][j] = log(f[i][j]);
    		}
    		memset(dp,-INF,sizeof(dp));
    		for(int j = 0; j < 3; j++)
    			dp[1][j] = st[j] + f[1][j];
    
    		for(int i = 2; i <= n; i++)
    		{
    			for(int j = 0; j < 3; j++)
    			{
    				for(int k = 0; k < 3; k++)
    				{
    					double t = dp[i-1][k] + ww[k][j] + f[i][j];
    					if(dp[i][j] < t)
    					{
    						dp[i][j] = t;
    						pre[i][j] = k;
    					}
    				}
    			}
    		}
    		int p = n;
    		int c;
    		double Max = -INF;
    
    		for(int j = 0; j < 3; j++)
    		{
    			if(dp[n][j] > Max)
    			{
    				Max = dp[n][j];
    				c = j;
    			}
    		}
    
    		while(!sta.empty()) sta.pop();
    
    		sta.push(c);
    		while(pre[p][c] != -1)
    		{
    			sta.push(pre[p][c]);
    			c = pre[p][c];
    			p--;
    		}
    
    		printf("Case #%d:
    ",item);
    		while(!sta.empty())
    		{
    			if(sta.top() == 0)
    				printf("Sunny
    ");
    			if(sta.top() == 1)
    				printf("Cloudy
    ");
    			if(sta.top() == 2)
    				printf("Rainy
    ");
    			sta.pop();
    		}
    	}
    	return 0;
    }
    


  • 相关阅读:
    类 2020年8月19
    随便一写,明天改正
    os模块 2020年8月16
    time 模块 2020年8月16
    collections模块 2020年8月16
    正则跟re模块内容2020年8月16日
    【C++设计模式二】C++工厂模式
    【C++设计模式一】C++简单工厂模式
    【01-springmvc快速入门、组件解析】
    03-【Spring 的 AOP】
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6692992.html
Copyright © 2020-2023  润新知