HDU 4865 Peter's Hoby (probability DP)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4865


There are three kinds of weather and Four Leaf states. The two tables are given, respectively, the probability of leaf state in each weather and the probability of today's weather to tomorrow's weather. The status of the leaves in N days is given to output the most likely weather sequence.


Thought: WL [I] [J] indicates the probability that the weather is I and the leaves are J. ww [I] [J] indicates that the weather today is I. The probability that the weather tomorrow is J, st [I] indicates the probability that the weather on the first day is I.

For the leaf sequence {A1, a2 ...... an}, there is a weather sequence {B1, B2 ...... BN }, then the total probability G [N] = sT [B1] * WL [B1] [a1] * ww [B1] [B2] * WL [B2] [a2] *...... * ww [bn-1] [bn] * WL [bn] [an]. That is, 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]). The sequence corresponding to the maximum value of log (G [N]) is the weather sequence.

It is equivalent to giving a matrix of N * 3, representing n days. There are 3 kinds of weather every day. From the first line to the nth line, we can find a longest path and output path.


# 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 # DEFINE _ ll _ int64 # define EPS 1e-12 # define PI ACOs (-1.0) using namespace STD; const int INF = 0x3f3f3f; 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 () {(); 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 ++) {// The leaf status on the current day is fixed, the current F [I] [J] indicates the probability that the weather on day I is J under fixed leaf conditions. // Therefore, F [I] [J] indicates each type of weather. Ratio of data to the total weather: 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: \ n", item ); while (! Sta. empty () {If (Sta. top () = 0) printf ("Sunny \ n"); If (Sta. top () = 1) printf ("Cloudy \ n"); If (Sta. top () = 2) printf ("Rainy \ n"); Sta. pop () ;}} return 0 ;}


HDU 4865 Peter's Hoby (probability DP)

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.