const int maxn = 100010; double dp[maxn][3]; int ipt[maxn], p[maxn][3]; char s[10]; double d1[][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 d2[][3] = { {0.5, 0.375, 0.125}, {0.25, 0.125, 0.625}, {0.25, 0.375, 0.375} }; map<string, int> mp; char to[][10] = {"Sunny", "Cloudy", "Rainy"}; int main() { mp["Dry"] = mp["Sunny"] = 0; mp["Dryish"] = mp["Cloudy"] = 1; mp["Damp"] = mp["Rainy"] = 2; mp["Soggy"] = 3; int T, n; RI(T); FE(kase, 1, T) { CLR(p, -1); RI(n); REP(i, n) { RS(s); ipt[i] = mp[s]; } dp[0][0] = log(0.63) + log(d1[0][ipt[0]]); dp[0][1] = log(0.17) + log(d1[1][ipt[0]]); dp[0][2] = log(0.2) + log(d1[2][ipt[0]]); FF(i, 1, n) { REP(j, 3) { dp[i][j] = -1e10; REP(k, 3) { double pre = dp[i - 1][k] + log(d2[k][j]) + log(d1[j][ipt[i]]); if (pre > dp[i][j]) { dp[i][j] = pre; p[i][j] = k; } } } } double Max = -1e10; int c = 0; REP(j, 3) if (dp[n - 1][j] > Max) { Max = dp[n - 1][j]; c = j; } stack<int> sk; int r = n - 1; while (r >= 0) { sk.push(c); c = p[r--][c]; } printf("Case #%d:\n", kase); while (!sk.empty()) { printf("%s\n", to[sk.top()]); sk.pop(); } } return 0; }
原文地址:http://blog.csdn.net/wty__/article/details/38063413