AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上进行行走,每次走到下一个能够走的结点。

14378527 11468 Substring Accepted C++ 0.585 2014-10-19 10:35:00

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 444;
const int max_size = 66;
const int maxd = 111;
int M,Case = 1;
int ch[max_size];
double P[max_size];
int index(int c){
if(c >= 'A' && c <= 'Z') return c - 'A';
if(c >= 'a' && c <= 'z') return c - 'a' + 26;
if(c >= '0' && c <= '9') return c - '0' + 52;
}
struct Trie{
int next[maxn][max_size];
int fail[maxn];
int val[maxn];
int vis[maxn][maxd];
double dp[maxn][maxd];
int sz,root;
int newnode(){
val[sz] = 0;
memset(next[sz],-1,sizeof(next[sz]));
sz ++;
return sz - 1;
}
void init(){
sz = 0;
root = newnode();
memset(vis,0,sizeof(vis));
return;
}
void insert(char *str){
int now = root;
int L = strlen(str);
for(int i = 0; i < L; i++){
int e = index(str[i]);
if(next[now][e] == -1)
next[now][e] = newnode();
now = next[now][e];
}
val[now] = 1;
return;
}
void build(){
queue<int>q;
fail[root] = root;
for(int i = 0; i < max_size ; i++)
if(next[root][i] != -1){
fail[next[root][i]] = root;
q.push(next[root][i]);
}
else
next[root][i] = root;
while(!q.empty()){
int now = q.front(); q.pop();
for(int i = 0; i < max_size ; i++){
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else{
q.push(next[now][i]);
fail[next[now][i]] = next[fail[now]][i];
val[next[now][i]] |= val[fail[next[now][i]]];
}
}
}
return;
}
double DP(int pos,int L){
if(!L) return 1.0;
if(vis[pos][L])
return dp[pos][L];
vis[pos][L] = 1;
double &ans = dp[pos][L];
ans = 0.0;
for(int i = 0; i < M; i++){
int e = ch[i];
if(!val[next[pos][e]])
ans += P[i] * DP(next[pos][e],L - 1);
}
return ans;
}
}ac;
int main(){
int T;
scanf("%d",&T);
while(T--){
ac.init();
int K;
scanf("%d",&K);
for(int i = 0; i < K; i++){
char str[maxn];
scanf("%s",str);
ac.insert(str);
}
ac.build();
scanf("%d",&M);
for(int i = 0; i < M; i++){
char str[maxn];
scanf("%s%lf",str,&P[i]);
ch[i] = index(str[0]);
}
int L;
scanf("%d",&L);
double ans = ac.DP(0,L);
printf("Case #%d: %.6f\n",Case++,ans);
}
return 0;
}

【UVA】11468-Substring(AC自己主动机)的更多相关文章

  1. uva 11468 - Substring(AC自己主动机+概率)

    题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...

  2. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

  3. UVA 10679 I love Strings!!!(AC自己主动机)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. 【UVA】1449-Dominating Patterns(AC自己主动机)

    AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...

  5. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  6. HDOJ 5384 Danganronpa AC自己主动机

     AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java ...

  7. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

  8. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. hdu5737

    首先思考一个朴素的做法 将b[]维护成一个线段树套有序表,每次修改a[]用线段树+lazy tag 并在线段树的子区间上在有序表中二分更新这段区间中a[i]>=b[i]的值,复杂度O(nlog^ ...

  2. background-position 用法介绍

    转自:http://blog.csdn.net/jeamking/article/details/5617088   语法: background-position : length || lengt ...

  3. paramiko 使用总结(SSH 操作远端机器)

    1.用户名.密码登陆方式 import paramikoparamiko.util.log_to_file('paramiko.log') # 记录日志文件ssh = paramiko.SSHClie ...

  4. 操作JSON————精品

    使用背景: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原 ...

  5. centos7下配置samba,win10访问

    yum install -y samba samba-client 更改配置 [root@abcd mnt]# cat /etc/samba/smb.conf [global] workgroup = ...

  6. 正则 lazy

  7. CodeForces - 990G GCD Counting

    Discription You are given a tree consisting of nn vertices. A number is written on each vertex; the ...

  8. 【Heap-Dijkstra】【分层图】bzoj2763 [JLOI2011]飞行路线

    建立k+1张图, 在图与图之间,若在原图中x到y有边,就建立从 第i层的x 到 i+1层的y 建边,权值为0.代表一次免费机会. 由于一旦到了第i+1层的图里,则无法回到之前的层,所以免费最多只有k次 ...

  9. 13test04:捕鱼

    #include<iostream> using namespace std; int main() { int fish_sum,flag=1; for(fish_sum=6;flag; ...

  10. Eclipse快速补全快捷键Ctrl+1修改为Android Studio的Alt+Enter

    步骤: Window ->Preferences->key-> type filter text 下输入quick fix(这个是快速补全的快捷键)改为Alt+Enter 下面的wh ...