标签:des style blog http java color


1 3 Dry Damp Soggy
Case #1: Sunny Cloudy RainyHintLog is useful.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define MAXN 100005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-10
typedef long long ll;
using namespace std;
int n,m,flag,cnt,tot,test=0;
int num[55],pre[55][4];
double dp[55][4];
char s[50];
double lea[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 wea[3][3]=
{
{0.5, 0.375, 0.125},
{0.25, 0.125, 0.625},
{0.25, 0.375, 0.375}
};
char res[3][15]=
{
"Sunny","Cloudy","Rainy"
};
void output(int x,int y)
{
if(x==0) return ;
output(x-1,pre[x][y]);
printf("%s\n",res[y]);
}
void solve()
{
int i,j,k,t,id;
double ma,tmp;
dp[1][0]=0.63*lea[0][num[1]];
dp[1][1]=0.17*lea[1][num[1]];
dp[1][2]=0.2*lea[2][num[1]];
for(i=2;i<=n;i++)
{
for(j=0;j<3;j++)
{
ma=-1;
for(k=0;k<3;k++)
{
tmp=dp[i-1][k]*wea[k][j]*lea[j][num[i]];
if(ma<tmp)
{
ma=tmp; id=k;
}
}
dp[i][j]=ma; pre[i][j]=id;
}
}
ma=-1;
for(j=0;j<3;j++)
{
if(dp[n][j]>ma)
{
ma=dp[n][j]; id=j;
}
}
printf("Case #%d:\n",++test);
output(n,id);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(strcmp(s,"Dry")==0) num[i]=0;
else if(strcmp(s,"Dryish")==0) num[i]=1;
else if(strcmp(s,"Damp")==0) num[i]=2;
else num[i]=3;
}
solve();
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define MAXN 100005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-10
typedef long long ll;
using namespace std;
int n,m,flag,cnt,tot,test=0;
int num[55],pre[55][4];
double dp[55][4];
char s[50];
double lea[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 wea[3][3]=
{
{0.5, 0.375, 0.125},
{0.25, 0.125, 0.625},
{0.25, 0.375, 0.375}
};
char res[3][15]=
{
"Sunny","Cloudy","Rainy"
};
void output(int x,int y)
{
if(x==0) return ;
output(x-1,pre[x][y]);
printf("%s\n",res[y]);
}
void solve()
{
int i,j,k,t,id;
double ma,tmp;
dp[1][0]=log(0.63)+lea[0][num[1]];
dp[1][1]=log(0.17)+lea[1][num[1]];
dp[1][2]=log(0.2)+lea[2][num[1]];
for(i=2;i<=n;i++)
{
for(j=0;j<3;j++)
{
ma=-INF;
for(k=0;k<3;k++)
{
tmp=dp[i-1][k]+wea[k][j]+lea[j][num[i]];
if(ma<tmp)
{
ma=tmp; id=k;
}
}
dp[i][j]=ma; pre[i][j]=id;
}
}
ma=-INF;
for(j=0;j<3;j++)
{
if(dp[n][j]>ma)
{
ma=dp[n][j];
id=j;
}
}
printf("Case #%d:\n",++test);
output(n,id);
}
int main()
{
int i,j,t;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
lea[i][j]=log(lea[i][j]);
if(j<3) wea[i][j]=log(wea[i][j]);
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(strcmp(s,"Dry")==0) num[i]=0;
else if(strcmp(s,"Dryish")==0) num[i]=1;
else if(strcmp(s,"Damp")==0) num[i]=2;
else num[i]=3;
}
solve();
}
return 0;
}
hdu 4865 Peter's Hobby (隐马尔可夫模型 dp),布布扣,bubuko.com
hdu 4865 Peter's Hobby (隐马尔可夫模型 dp)
标签:des style blog http java color
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/38069917