Question Link
Here are two tables. The first one is the possibility of four kinds of humidity in three kinds of weather. The second table shows the possibility of three types of weather today in three weather conditions that appeared yesterday. Then we will give you the humidity of the past few days, tell you the possibility of three kinds of weather on the first day, and let you find the most likely sequence of weather.
Idea: Define the leaf humidity on day I as hum [I]. Day I, the maximum probability of weather J is DP [I] [J]. Wealea [I] [J] indicates the probability that the weather is I leaf = J, and weawea [I] [J] indicates the probability that the weather today is I tomorrow is J, st [I] indicates the probability that the weather on the first day is I. Pre [I] [J]: The day I, when the weather is J, the most likely weather of the day before. FIR [I] indicates the probability that the weather on 1st days is I.
For the existing leaf sequence {A1, a2 ...... an}, there is a weather sequence {B1, B2 ...... BN }, then the total probability DP [N] [J] = max (FIR [B1] * wealea [B1] [a1] * weawea [B1] [B2] * wealea [B2] [A2] * ...... * weawea [bn-1] [bn] * wealea [bn] [an]). If the data size is too small, precision may be lost. Therefore, you can use log to convert multiplication into addition, that is, log () = Log (FIR [B1]) + Log (wealea [B1] [a1]) + Log (weawea [B1] [B2]) + Log (wealea [B2] [a2]) + ...... + Log (weawea [bn-1] [bn]) + Log (wealea [bn] [an]). The sequence corresponding to the maximum value of log () is the weather sequence.
Official question:
1 // E 2 # include <cstdio> 3 # include <cstring> 4 # include <vector> 5 # include <iostream> 6 # include <string> 7 # include <algorithm> 8 # include <cmath> 9 10 using namespace STD; 11 12 string STR; 13 vector <int> VEC; 14 double wealea [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 }}; 15 double weawea [3] [3] = {0.5, 0.375, 0.125}, {0.25, 0.125, 0.625 },{ 0.25, 0.375, 0.375 }}; 16 doubl E fir [3] = {0.63, 0.17, 0.2}; 17 double DP [53] [5]; 18 double F [53] [5]; 19 int pre [53] [5]; 20 int hum [53]; 21 22 void solve () 23 {24 for (INT I = 0; I <3; I ++) 25 FIR [I] = Log (FIR [I]); 26 for (INT I = 0; I <3; I ++) 27 For (Int J = 0; j <3; j ++) 28 weawea [I] [J] = Log (weawea [I] [J]); 29} 30 void Init () 31 {32 memset (PRE,-1, sizeof (pre); 33 memset (F, 0, sizeof (f )); 34 For (INT I = 0; I <53; I ++) 35 for (Int J = 0; j <3; j ++) 36 DP [I] [J] =-9999999; 37 Vec. clear (); 38} 39 int main () 40 {41 int t, n; 42 CIN> T; 43 solve (); 44 for (INT I = 1; I <= T; I ++) 45 {46 CIN> N; 47 Init (); 48 for (Int J = 0; j <n; j ++) 49 {50 CIN> STR; 51 if (STR = "dry") hum [J] = 0; 52 else if (STR = "dryish ") hum [J] = 1; 53 else if (STR = "damp") hum [J] = 2; 54 else if (STR = "s Oggy ") hum [J] = 3; 55} 56 for (Int J = 0; j <n; j ++) 57 {58 Double X = 0; 59 for (int K = 0; k <3; k ++) 60 {61 F [J] [k] = wealea [k] [hum [J]; 62 X + = f [J] [k]; 63} 64 for (int K = 0; k <3; k ++) 65 f [J] [k]/= x; 66} 67 for (Int J = 0; j <n; j ++) 68 for (int K = 0; k <3; k ++) 69 F [J] [k] = Log (F [J] [k]); 70 for (Int J = 0; j <3; j ++) 71 DP [0] [J] = f [0] [J] + FIR [J]; 72 for (Int J = 1; J <n; j ++) 73 for (int K = 0; k <3; k ++) // Today 74 for (INT h = 0; H <3; h ++) // Yesterday 75 {76 double X1 = DP [J-1] [H] + weawea [H] [k] + F [J] [k]; 77 If (DP [J] [k] <X1) 78 {79 DP [J] [k] = x1; 80 pre [J] [k] = h; 81} 82} 83 double Maxx =-9999999; 84 int S = 0, E = n-1; 85 for (Int J = 0; j <3; j ++) 86 {87 If (DP [n-1] [J]> Maxx) 88 {89 Maxx = DP [n-1] [J]; 90 s = J; 91} 92} 93 while (pre [E] [s]! =-1) 94 {95 Vec. push_back (s); 96 S = pre [E] [s]; 97 e --; 98} 99 Vec. push_back (s); 100 cout <"case #" <I <":" <Endl; 101 For (Int J = n-1; j> = 0; J --) 102 {103 If (VEC [J] = 0) cout <"Sunny" <Endl; 104 If (VEC [J] = 1) cout <"Cloudy" <Endl; 105 if (VEC [J] = 2) cout <"Rainy" <Endl; 106} 107 return 0; 109}
View code