首页 > 代码库 > hdu 3689 Infinite monkey theorem

hdu 3689 Infinite monkey theorem

TMD这些神奇的猴子。。。

DP里面用KMP的next的数组来搞一搞,(不是很会,一开始想这样搞,然而思路很乱,就弃疗了,,,DP太虚了)

 1 #include<bits/stdc++.h>
 2 #define N 1000005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<0 || ch>9) {if (ch==-) f=-1; ch=getchar();}
10     while (ch>=0 && ch<=9) {x=x*10+ch-0; ch=getchar();}
11     return x*f;
12 }
13 const int MAXN = 30;
14 char s[MAXN];
15 int fail[MAXN];
16 int n,m,L;
17 char c[MAXN];
18 double p[MAXN];
19 double dp[1010][MAXN];
20 void cal()
21 {
22     fail[1]=0; int fix=0;
23     for (int i=2; i<=L; i++)
24     {
25         while (fix && s[fix+1]!=s[i]) fix=fail[fix];
26         if (s[fix+1]==s[i]) fix++;
27         fail[i]=fix;
28     }
29 }
30 int main()
31 {
32     while (scanf("%d %d\n",&m,&n))
33     {
34         memset(dp,0,sizeof(dp)); 
35         if (!m && !n) break;
36         for (int i=1; i<=m; i++) scanf("%c %lf\n",&c[i],&p[i]);
37         scanf("%s",s+1); 
38         L=strlen(s+1); cal();
39         dp[0][0]=1;
40         for (int i=0; i<n; i++)
41             for (int j=0; j<L; j++)
42                 for (int k=1; k<=m; k++)
43                 {
44                     int fix=j;
45                     while (fix && s[fix+1]!=c[k]) fix=fail[fix];
46                     if (s[fix+1]==c[k]) fix++;
47                     dp[i+1][fix]+=dp[i][j]*p[k];
48                 }
49         double ans=0;
50         for (int i=0; i<=n; i++) ans+=dp[i][L];
51         printf("%.2lf%%\n",ans*100);//system("pause");
52     }
53     return 0;
54 }

 

hdu 3689 Infinite monkey theorem