UVA11468 Substring
思路
AC自动机+概率dp
设f[i][j]表示当前在第i位在AC自动机的第j个节点,满足条件的概率
AC自动机上的一个节点能被转移到当且仅当这个节点不是中止节点且无法通过fail指针跳到任何一个中止节点
答案就是\(\sum_{x\in node} dp[L][x]\)
注意本题字符集为09,AZ,a~z
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int Trie[410][64],fail[410],Nodecnt,root,ban[410],a[410],K,N,L;
double f[110][410],p[410];
char s[410];
int tonum(char c){
if(c>='a'&&c<='z')
return c-'a';//0~25
else if(c>='A'&&c<='Z')
return c-'A'+26;//26~51
else if(c>='0'&&c<='9')
return c-'0'+52;
}
void insert(char *s,int len){
int o=root;
for(int i=0;i<len;i++){
if(!Trie[o][tonum(s[i])])
Trie[o][tonum(s[i])]=++Nodecnt;
o=Trie[o][tonum(s[i])];
}
ban[o]=true;
}
queue<int> q;
void get_AC(void){
for(int i=0;i<64;i++){
if(Trie[root][i]){
fail[Trie[root][i]]=root;
if(ban[fail[Trie[root][i]]])
ban[Trie[root][i]]=true;
q.push(Trie[root][i]);
}
}
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<64;i++){
if(Trie[x][i]){
fail[Trie[x][i]]=Trie[fail[x]][i];
if(ban[fail[Trie[x][i]]])
ban[Trie[x][i]]=true;
q.push(Trie[x][i]);
}
else
Trie[x][i]=Trie[fail[x]][i];
}
}
}
double dp(void){
int o=root;
f[0][root]=1.0;
for(int i=0;i<L;i++){
for(int j=0;j<=Nodecnt;j++)
if(!ban[j])
for(int k=1;k<=N;k++)
if(!ban[Trie[j][tonum(a[k])]])
f[i+1][Trie[j][tonum(a[k])]]+=f[i][j]*p[k];
}
double ans=0;
for(int i=0;i<=Nodecnt;i++)
ans+=f[L][i];
return ans;
}
void init(void){
Nodecnt=0;
root=0;
memset(Trie,0,sizeof(Trie));
memset(fail,0,sizeof(fail));
memset(f,0,sizeof(f));
memset(ban,0,sizeof(ban));
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
int T,cnt=0;
scanf("%d",&T);
while(T--){
cnt++;
init();
scanf("%d",&K);
for(int i=1;i<=K;i++){
scanf("%s",s);
insert(s,strlen(s));
}
scanf("%d",&N);
for(int i=1;i<=N;i++){
char c=getchar();
while((c<'a'||c>'z')&&(c<'A'||c>'Z')&&(c<'0'||c>'9'))
c=getchar();
// printf("c=%c\n",c);
a[i]=c;
scanf("%lf",&p[i]);
}
scanf("%d",&L);
get_AC();
printf("Case #%d: %.6lf\n",cnt,dp());
}
return 0;
}
UVA11468 Substring的更多相关文章
- UVA11468 Substring --- AC自动机 + 概率DP
UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...
- UVA-11468 Substring(AC自动机+DP)
题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- substring的用法
public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beginIndex 处开 ...
- jQuery之常用且重要方法梳理(target,arguments,slice,substring,data,trigger,Attr)-(一)
1.jquery data(name) data() 方法向被选元素附加数据,或者从被选元素获取数据. $("#btn1").click(function(){ $(" ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
随机推荐
- vector排序
// VectorSort.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- _T宏的使用
来源自百度. 他的作用是让你的程序支持Unicode编码, 因为Windows使用两种字符集ANSI和UNICODE, 前者就是通常使用的单字节方式, 但这种方式处理像中文这样的双字节字符不方便, ...
- 怎么访问不在网站目录下文件(iis虚拟目录设置)
很多时候,上传的文件多了,架设服务器当初设定的主目录所在盘空间往往就不够了,怎么办?这就需要设置虚拟目录.虚拟目录就是将其他目录以映射的方式虚拟到该FTP服务器的主目录下,这样,一个FTP服务器的主目 ...
- Finalize方法的生成
Finalize在c#编程语言中需要特殊语法,因此,c#要求在类名前加~符号来定义Finalize方法:例如 internal class FinalizeDemo { ~FinalizeDemo() ...
- LeetCode 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- Spring之IOC注入
注入 spring依赖注入 set方法: <property name="属性名" values ="值">--ref="对象名" ...
- Fiddler过滤
1.REGEX:\.(js|css|js|png|gif|\?.*|css\?.*)$ 2..css .js .gif .png .jpg .swf
- IO调度算法的选择
一) I/O调度程序的总结 1) 当向设备写入数据块或是从设备读出数据块时,请求都被安置在一个队列中等待完成. 2) 每个块设备都有它自己的队列. 3) I/O调度程序负责维护这些队列的顺序,以更有效 ...
- 认识Charles-proxy 抓包工具
1.为什么不用 Fiddler 抓包工具? 在这里说明一下,因为Fiddler 抓包工具使用C#语言写的,不能在 MAC 上运行,而 Charles-proxy 他是 java 开发的,可以 ...
- Vue使用Typescript开发编译时提示“ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined”的解决方法
使用Typescript开发Vue,一切准备就绪.但npm start 时,提示“ ERROR in ./src/main.tsModule build failed: TypeError: Cann ...