【UVA】11468-Substring(AC自己主动机)
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自己主动机)的更多相关文章
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- UVa 11468 Substring (AC自动机+概率DP)
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...
- UVA 10679 I love Strings!!!(AC自己主动机)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【UVA】1449-Dominating Patterns(AC自己主动机)
AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...
- 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 ...
- HDOJ 5384 Danganronpa AC自己主动机
AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java ...
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
随机推荐
- 使用rosbag记录openni2_launch消息
首先看教程 http://wiki.ros.org/openni_launch/Tutorials/BagRecordingPlayback http://wiki.ros.org/Bags http ...
- Python2.5/2.6实用教程:基础篇(转)
介绍 本文方便有经验的程序员进入Python世界.本文适用于python2.5/2.6版本. Deparecated:Python3 和 python2.x语法不同.如果你正在用python3,请参考 ...
- [xampp] phpmyadmin 设置登录密码
$ cd /opt/lampp/bin $ ./mysqladmin -u root password 'new_password' $ vim ../phpmyadmin/config.inc.ph ...
- (1) Flutter android studio安装
Flutter由两部分组成 Flutter引擎和Flutter框架 Flutter引擎由C++编写,在android上通过NDK编译,在ios上通过llvm编译 Flutter框架由dart编写 1. ...
- 利用ipv6技术,废旧笔记本变成互联网server
如果你家的路由器已经get到了ipv6地址,并且你家的电脑也获取了有效的ipv6地址,在广域网的设备可以访问到.那恭喜你,再配合我这个ddns,你可以完美地把你家的电脑当服务器使用. 1.确保你家的宽 ...
- 福州3中集训day5
数论,zld神犇认为我们都学过数论的,讲了一波高端(入门?)操作,从扩展欧几里得开始,同余方程诸如此类,早晚得重修.连课件都没,拿着画图讲了一上午 sro_zld_orz 具体内容都记在本上. 还是说 ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - M Lucky 7
Lucky 7 Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a positive integer se ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- 后门工具dbd
后门工具dbd dbd功能类似于Netcat,但提供强大的加密功能,支持AES-CBC-128和HMAC-SHA1加密.该工具可以运行在类Unix和Windows系统中.渗透测试人员首先使用该工具 ...
- 【记忆化搜索】bzoj1079 [SCOI2008]着色方案
#include<cstring> #include<cstdio> using namespace std; #define MOD 1000000007 typedef l ...