【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 ...
随机推荐
- 【hdoj_2124】RepairTheWall
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2124 思路:贪心法.由于要求所需的块儿(block)的最小数目,先把所有的块儿加起来,看看大小是否> ...
- Tomcat 服务器基本知识
Tomcat下载安装和配置 下载 下载地址: http://tomcat.apache.org tomcat服务器分为很多版本, 其中包括windows版和linux版 ...
- 让webstorm 识别vue cli3项目中的@路径别名
在setting -> languages&frameworks -> webpack里选择配置文件路径为 node_modules/@vue/cli-service/webpac ...
- 山东省第六届省赛 H题:Square Number
Description In mathematics, a square number is an integer that is the square of an integer. In other ...
- (7)python 函数和lambda表达式
一.函数定义和调用 1.定义函数用def 2.函数注释 在函数体里的开头写上字符串,可以起到说明的作用 可以用函数名.__doc__的方式读取在函数开头加的字符串(双下划线) 内建的help()函数也 ...
- 并查集【p1197】[JSOI2008]星球大战
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...
- Integer 类和 int 的区别
http://www.cnblogs.com/ysocean/p/8075676.html public static void main(String[] args) { Integer i ...
- CodeForces 786B Legacy(线段树优化建图+最短路)
[题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- 【dfs】bzoj3563 DZY Loves Chinese
因为我们可以通过把某一行读到末尾来获取真正的K,所以把它和假K异或之后就是之前联通的次数(异或的逆运算为其本身).最后一次的暴力一下. #include<cstdio> #include& ...
- 10.2(java学习笔记)JDBC事务简述
一.事务 事务是指作为一系列操作组成的一个整体,该整体只有两种状态,要么全部执行,要么全部不执行. 当组成这个事务的所有语句都执行成功则该事务执行,只要有一条语句执行失败则该事务不执行. 假设这里有一 ...