UVA11468 Substring

摘要:
乍一看,似乎没有办法转移这个问题,但这只是将两个知识点挤在一起的问题。要判断多个字符串是否是一个字符串的子字符串,首先考虑AC自动机:将模板字符串构建为AC自动机,然后对其进行dp。AC自动机上从每个节点到根的路径表示前缀,因此模板字符串可以匹配节点,文本字符串可以匹配长度。然后很容易想到转移:。

传送


这题乍一看觉得无从下手,但只不过是把两个知识点捏到一块罢了。


判断多个串是否为一个串的子串,首先想到AC自动机:把(K)个模板串建成AC自动机,然后在上面dp(记忆化搜索)。
AC自动机上的每一个节点到根的路径都代表一个前缀,所以令(dp[i][j])表示模板串匹配到节点(i),文本串匹配到长度(j)时的"no"可能性。
那么转移就很好想了:(dp[u][j] = sum_{c in 字符集} p[c] * dp[v][j+1])。只要不往模板串代表的节点转移即可。


需要注意的是,如果一个节点为一个模板串的结束位置,那么他在fail树上的所有后代节点都是该模板串的结束位置,所以构造fail指针的时候要(num[u] |= num[v])(v)(u)的后代)。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const db eps = 1e-8;
const int maxt = 70;
const int maxN = 4e4 + 5;
const int maxn = 105;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}

int n, m, L;
char s[maxn];
db p[maxt];

int ch[maxN][maxt], f[maxN], num[maxN], cnt = 0;
In void Clear(int x) {Mem(ch[x], 0), f[x] = num[x] = 0;}
In int C(char c)
{
	if(isdigit(c)) return c - '0';
	else if(islower(c)) return c - 'a' + 11;
	else return c - 'A' + 37;
}
In void insert(char* s)
{
	int m = strlen(s), now = 0;
	for(int i = 0; i < m; ++i)
	{
		int c = C(s[i]);
		if(!ch[now][c]) Clear(++cnt), ch[now][c] = cnt;
		now = ch[now][c];
	}
	num[now] = 1;
}
In void build()
{
	queue<int> q;
	for(int i = 0; i < maxt; ++i) if(ch[0][i]) q.push(ch[0][i]);
	while(!q.empty())
	{
		int now = q.front(); q.pop();
		for(int i = 0; i < maxt; ++i)
			if(ch[now][i]) f[ch[now][i]] = ch[f[now]][i], q.push(ch[now][i]);
			else ch[now][i] = ch[f[now]][i];
		num[now] |= num[f[now]];
	}
}

bool vis[maxN][maxn];
db dp[maxN][maxn];
In db dfs(int now, int len)		//记忆化搜索 
{
	if(len == L + 1) return 1;
	if(vis[now][len]) return dp[now][len];
	vis[now][len] = 1; db ret = 0;
	for(int i = 0; i < maxt; ++i)
		if(!num[ch[now][i]]) ret += p[i] * dfs(ch[now][i], len + 1);
	return dp[now][len] = ret;
}

In void init()
{
	Mem(p, 0), Mem(vis, 0), Mem(dp, 0);
	Clear(cnt = 0);
}

int main()
{
	int T = read();
	for(int id = 1; id <= T; ++id)
	{
		init();
		n = read();
		for(int i = 1; i <= n; ++i) 
		{
			scanf("%s", s);
			insert(s);
		}
		build();
		m = read();
		for(int i = 1; i <= m; ++i)
		{
			char cc[2]; db tp;
			scanf("%s%lf", cc, &tp);
			p[C(cc[0])] = tp;
		}
		L = read();
		printf("Case #%d: %.6lf
", id, dfs(0, 1));
	}
	return 0;
}

免责声明:文章转载自《UVA11468 Substring》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MDK5.29,5.30,5.31和各种软件包镜像下载(2020-07-03)Windows平台最方便最易用的法语输入法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)

本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer、PropertyNamer、PropertyCopier。 PropertyTokenizer: 主要用来解析Mybatis使用过程中遇到的类似"order[0].items[0].name"这种由"."和"[ ]"组成的表达式: public class Pr...

(4.16)存储过程的加密与解密

(4.16)存储过程的加密与解密 存储过程加密概念:无法查看到过程的内容,也无法右键生成create等脚本 适用范围:从SQL2008到SQL2016,2017和2019还没试过,2005反正不行 【1】先决条件DAC 【1.1】远程DAC启用(如果本机就不用这一步) --执行下面的SQL可以启用远程使用DAC: Usemaster GO /*0 = Al...

C#计算字符串中子串出现次数的另类方法

转自http://www.zu14.cn/2011/04/13/donet-csharp-calculate-sub-string-counts-of-string/ /// <summary>/// 计算字符串中子串出现的次数/// </summary>/// <param name="str">字符串</par...

js-判断字符串中是否存在emoji表情

1 function isEmojiCharacter(substring) { 2 for(var i = 0; i < substring.length; i++) { 3 var hs = substring.charCodeAt(i); 4...

java读取html文件,截取&amp;lt;body&amp;gt;标签中内容

1 public String readfile(String filePath){ 2 File file = new File(filePath); 3 InputStream input = null; 4 try { 5 input = new FileI...

sqlserver 之 将查询结果变为json字符串

GO /****** Object: StoredProcedure [dbo].[SerializeJSON] Script Date: 6/4/2019 3:58:23 PM 将查询结果变为json字符串 ******/SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PRO...