Code:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=100;
const int maxd=400+3;
const int sigma=65;
double d[maxd][103],perc[sigma+3];
int ch[maxd][sigma+1],end1[maxd],f[maxd],last[maxd];
char A[30],mapp[sigma+3];
int cnt;
queue<int>Q;
int idx(char s){
if(s>='a'&&s<='z')return s-'a';
if(s>='A'&&s<='Z')return s-'A'+26;
return s-'0'+52;
}
struct V
{
void insert(char p[]){
int n=strlen(p);
int cur=0;
for(int i=0;i<n;++i){
int c=idx(p[i]);
if(!ch[cur][c])ch[cur][c]=++cnt;
cur=ch[cur][c];
}
end1[cur]=1,last[cur]=1;
}
void getfail(){
for(int i=0;i<sigma;++i)if(ch[0][i])Q.push(ch[0][i]);
while(!Q.empty()){
int r=Q.front();Q.pop();
for(int i=0;i<sigma;++i){
int u=ch[r][i];
if(!u){ch[r][i]=ch[f[r]][i];continue;}
Q.push(u);
int v=f[r];
while(v&&!ch[v][i])v=f[v];
f[u]=ch[v][i];
if(last[f[u]])last[u]=1;
}
}
}
double dp(int j,int L,int l,int n){
if(end1[j]||last[j])return 0.0;
if(L==l)return 1.0;
if(d[j][L]>=0)return d[j][L];
double ans=0.0;
for(int i=1;i<=n;++i){
int c=idx(mapp[i]);
ans+=perc[i]*dp(ch[j][c],L+1,l,n);
}
d[j][L]=ans;
return ans;
}
}AC;
int main(){
int T;scanf("%d",&T);
for(int cas=1;cas<=T;++cas)
{
memset(end1,0,sizeof(end1)); memset(d,-1,sizeof(d));
memset(f,0,sizeof(f)); memset(last,0,sizeof(last));
memset(ch,0,sizeof(ch));
cnt=0;
int k;scanf("%d",&k);
for(int i=1;i<=k;++i){scanf("%s",A);AC.insert(A);}
int n;scanf("%d",&n);char g=getchar();
for(int i=1;i<=n;++i)
{scanf("%c",&mapp[i]); scanf("%lf",&perc[i]);g=getchar();}
int l;scanf("%d",&l);
AC.getfail();
double ans=AC.dp(0,0,l,n);
printf("Case #%d: ",cas);
printf("%.6f\n",ans);
}
return 0;
}

  

Substring Uva 11468_记忆化搜索 + AC自动机的更多相关文章

  1. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  2. UVA 11468 Substring (记忆化搜索 + AC自动鸡)

    传送门 题意: 给你K个模式串, 然后,再给你 n 个字符, 和它们出现的概率 p[ i ], 模式串肯定由给定的字符组成. 且所有字符,要么是数字,要么是大小写字母. 问你生成一个长度为L的串,不包 ...

  3. uva 707(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...

  4. UVa 10118 记忆化搜索 Free Candies

    假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...

  5. UVa 10400 记忆化搜索

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  6. BZOJ-3208|记忆化搜索-花神的秒题计划Ⅰ

    背景[backboard]: Memphis等一群蒟蒻出题中,花神凑过来秒题-- 描述[discribe]: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我 ...

  7. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  8. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  9. UVA - 1631 Locker 记忆化搜索

    题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...

随机推荐

  1. 0613pt-query-digest分析慢查询日志

    转自http://www.jb51.net/article/107698.htm 这篇文章主要介绍了关于MySQL慢查询之pt-query-digest分析慢查询日志的相关资料,文中介绍的非常详细,对 ...

  2. Sigma Function 数学 因子求和

    Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma ...

  3. [bzoj1500][NOI2005]维修数列_非旋转Treap

    维修数列 bzoj-1500 NOI-2005 题目大意:给定n个数,m个操作,支持:在指定位置插入一段数:删除一个数:区间修改:区间翻转.查询:区间和:全局最大子序列. 注释:$1\le n_{ma ...

  4. mybatis中useGeneratedKeys和keyProperty的使用

    领域模型主键属性是shopId,使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型shopId属性中,配置参考如下:<insert id=& ...

  5. POJ 3608

    1.计算P上y坐标值最小的顶点(称为 yminP )和Q上y坐标值最大的顶点(称为 ymaxQ). 2.为多边形在 yminP 和 ymaxQ 处构造两条切线 LP 和 LQ 使得他们对应的多边形位于 ...

  6. 一个表空间使用率查询sql的优化

    话不多说,直接上运行计划: SQL> set lines 500; SQL> set pagesize 9999; SQL> set long 9999; SQL> selec ...

  7. leetCode(30):Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. oracel表的分区

    1,创建表及分区 create table test ( ID VARCHAR2(32), MONTHS VARCHAR2(40), USERID VARCHAR2(20) ) partition b ...

  9. tflearn Training Step每次 We will run it for 10 epochs (the network will see all data 10 times) with a batch size of 16. n_epoch=10, batch_size=16

    Training TFLearn provides a model wrapper 'DNN' that can automatically performs a neural network cla ...

  10. axis2调用webservice教训

    总结教训,axis2client调用WS接口时url不能加?wsdl,而用cxf调用时则要加上. 今天用axis2的RpcServerClient调用https的webservice接口,在设置完op ...