标签:概率 regional produce keyboard ast 包含 compose character begin
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1702 Accepted Submission(s):
882
字符集中有一些字符(最多26个),给出每个字符的出现概率(它们的和保证为1)
再给出一个子串B
求:任给一个长度为N的字符串A(只能包含字符集中的字符),使得B是A的子串的概率。
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long ll; const int N=1005,M=15; int cn,n,m,fail[N]; char c[2],b[N],key[N]; double p[N],f[N][M]; void getFail(int n,char s[]){ fail[1]=0; for(int i=2;i<=n;i++){ int j=fail[i-1]; while(j&&s[j+1]!=s[i]) j=fail[j]; fail[i]=s[j+1]==s[i]?j+1:0; } } void dp(){ memset(f,0,sizeof(f)); f[0][0]=1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(f[i][j]) for(int k=1;k<=cn;k++){ int now=j; while(now&&b[now+1]!=key[k]) now=fail[now]; now+=b[now+1]==key[k]; f[i+1][now]+=f[i][j]*p[k]; } double ans=0; for(int i=1;i<=n;i++) ans+=f[i][m]; ans*=100; printf("%.2lf%%\n",ans); } int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d",&cn,&n)!=EOF&&(cn||n)){ for(int i=1;i<=cn;i++){ scanf("%s%lf",c,&p[i]); key[i]=c[0]; } scanf("%s",b+1); m=strlen(b+1); getFail(m,b); dp(); } }
HDU 3689 Infinite monkey theorem [KMP DP]
标签:概率 regional produce keyboard ast 包含 compose character begin
原文地址:http://www.cnblogs.com/candy99/p/6366221.html