Peter's Hoby
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 545 accepted submission (s): 237
Problem descriptionrecently, 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.cocould you know the weathers of these days most probably like in order? Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4865
There are three kinds of weather and four kinds of humidity, which give the probability of four kinds of humidity in each weather and the transfer probability between the weather and the weather. Now we provide a humidity sequence for N days, A weather sequence with the highest probability is required.
Train of Thought: simple probability DP, the transfer probability and state probability are all given. Set DP [I] [J] to indicate that day I is the maximum probability of day J, then pre [I] [J] records the sequence.
Assume that the day of I-1 is the J1 weather, the day I is the J2 weather, the probability of weather transfer is P1 [J1] [J2], the humidity of the day I is X, in the J2 weather, the probability of X humidity is P2 [J2] [X], then:
DP [I] [J2] = max (DP [I-1] [J1] * P1 [J1] [J2] * P2 [J2] [x]), use pre [I] [J2] to record which J1 transfers the maximum value.Finally, obtain the largestDP [N] [J], AccordingPreArray output path.
Note: The question says that because DP [N] [J] may be very small, double multiplication may result in precision loss, so log is used, but I have passed the multiplication directly, it may be luck. You should pay more attention to this problem in the future.
# Include <iostream> # include <string. h> # include <stdio. h> # include <algorithm> # include <math. h ># include <string >#include <map> # include <vector> # define maxn 55 using namespace STD; double DP [55] [3]; int pre [55] [3]; Map <string, int> MP; string STR; double P1 [3] [4] = {0.6, 0.2, 0.15, 0.05, 0.25, 0.3, 0.2, 0.25, 0.05, 0.1, 0.35}; double P2 [3] [3] = {0.5, 0.5, 0.375, 0.125, 0.25, 0.125, 0.625, 0.25, 0.375, 0.375}; void Init () {MP. insert (Make_pair ("dry", 0); MP. insert (make_pair ("dryish", 1); MP. insert (make_pair ("damp", 2); MP. insert (make_pair ("soggy", 3);} int main () {int ncase, T = 0; scanf ("% d", & ncase); Init (); while (ncase --) {printf ("case # % d: \ n", ++ T); int N; scanf ("% d", & N ); cin> STR; For (INT I = 0; I <= N; I ++) {for (Int J = 0; j <3; j ++) DP [I] [J] = 0;} int lab = MP [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 (INT I = 2; I <= N; I ++) {CIN> STR; int lab = MP [STR]; for (Int J = 0; j <3; j ++) {for (int 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 (INT 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 (INT I = n-1; I> = 0; I --) {If (ANS [I] = 0) printf ("Sunny \ n"); else if (ANS [I] = 1) printf ("Cloudy \ n "); else printf ("Rainy \ n") ;}} return 0 ;}