题目描述:

带有子串包含约束的最长公共子序列问题可以具体表述如下。
给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S。
S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长度为li,1≤i≤k。带有子串包含约束的最长公共子序列问题就是要找出X和Y的包含约束集S中所有字符串为其子串的最长公共子序列。

例如,如果给定的序列X和Y分别为X=actaagacct, Y=gacctacctc,子串包含约束集S={ata, tact},则子序列actacct是X和Y的一个无约束的最长公共子序列,而包含约束集S中所有字符串为其子串的一个最长公共子序列是atact 。

在本题中请特别关注子串与子序列的区别。字符串T=t1…tn的子串是一个形如T’=t1+i…tm+i的字符串,其中,0≤i,m+i≤n。例如,T=abcdefg,则bcd是T 的一个子串,而bce是T的一个子序列,但不是T 的子串。
设计一个算法,找出给定序列X和Y带有子串包含约束S的最长公共子序列。

输入:

第1行中给出正整数n,m,k,m<300, n<300, k<6。n和m分别表示给定序列X和Y的长度。k表示子串包含约束集S中共有k个字符串。
第2行中有k个整数li,0≤li≤300,1≤i≤k,分别表示子串包含约束集S中k个字符串的长度度。
第3行和第4行分别给出序列X和Y 。
接下来k行每行一个字符串Si

输出:

将计算出的X和Y带子串包含约束S的最长公共子序列的长度输出。

样例输入:

10 10 2
3 4
actaagacct
gacctacctc
ata
tact

样例输出:

5

题解:

AC自动机+序列自动机+哈希+记忆化搜索

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 310
#define maxk 10
#define maxcnt 30010
int sl[maxk], trie[maxcnt][60], fail[maxcnt], t1[maxn][60], t2[maxn][60], len, cnt;
char str1[maxn], str2[maxn], str[maxn];
int end[maxcnt];
inline void Insert(R int pos)
{
R int now = 0;
for (R int i = 1; i <= len; i++)
{
R int c = str[i] - 'A';
if (!trie[now][c]) now = trie[now][c] = ++cnt;
else now = trie[now][c];
}
end[now] |= pos;
}
std::queue<int> q;
inline void ACmach()
{
fail[0] = 0;
for (R int i = 0; i < 60; ++i)
if (trie[0][i]) q.push(trie[0][i]);
while (!q.empty())
{
R int now = q.front(); q.pop(); end[now] |= end[fail[now]];
for (R int i = 0; i < 60; ++i)
if (!trie[now][i]) trie[now][i] = trie[fail[now]][i];
else
{
fail[trie[now][i]] = trie[fail[now]][i];
q.push(trie[now][i]);
}
}
}
#define hashsize 9991023
#define INF 0x7fffffff
struct Hashtable
{
long long v; int dp;
Hashtable *next;
}*last[hashsize], mem[hashsize], *tot = mem;
inline Hashtable *Ha(R int a, R int b, R int now, R int s)
{
R long long key = ((((long long)a<<9|b)<< 11|now)<< 6|s);
for (R Hashtable *pos = last[key % hashsize]; pos; pos = pos -> next) if (pos -> v == key) return pos;
*++tot = (Hashtable){key, 0,last[key % hashsize]};
last[key % hashsize] = tot;
return tot;
}
int full;
int dfs(R int a, R int b, R int now, R int s)
{
s |= end[now];
R Hashtable *key = Ha(a, b, now, s);
if (key -> dp) return key->dp;
R int tmp = (s == full ? 0 : -INF);
for (R int i = 0; i < 60; ++i)
if (t1[a][i] && t2[b][i])
{
R int temp = dfs(t1[a][i], t2[b][i], trie[now][i], s);
cmax(tmp, temp);
}
return key->dp = tmp + 1;
}
int main()
{
R int n, m, k;
scanf("%d %d %d\n", &n, &m, &k);
full = (1 << k) - 1;
for (R int i = 0; i < k; ++i)
scanf("%d ", &sl[i]);
gets(str1 + 1);
gets(str2 + 1);
for (R int i = 0; i < k; ++i)
{
gets(str + 1);
len = sl[i];
Insert(1 << i);
}
ACmach();
memset(t1[n], 0, sizeof(t1[n]));
for (R int i = n; i; --i)
{
memcpy(t1[i - 1], t1[i], sizeof(t1[i]));
t1[i - 1][str1[i] - 'A'] = i;
}
memset(t2[m], 0, sizeof(t2[m]));
for (R int i = m; i; --i)
{
memcpy(t2[i - 1], t2[i], sizeof(t2[i]));
t2[i - 1][str2[i] - 'A'] = i;
}
R int ans = dfs(0, 0, 0, 0) - 1;
printf("%d\n", dmax(ans, 0));
return 0;
}
/*
10 10 2
3 4
actaagacct
gacctacctc
ata
tact
*/

【bzoj4136】[FJOI2015]带子串包含约束LCS问题的更多相关文章

  1. 「双串最长公共子串」SP1811 LCS - Longest Common Substring

    知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...

  2. SQL 2005 带自增列 带外键约束 数据导入导出

    1,生成建表脚本 选中要导的表,点右键-编写表脚本为-create到  ,生成建表脚本 2,建表(在新库),但不建外键关系 不要选中生成外键的那部分代码,只选择建表的代码 3,导数据,用SQL STU ...

  3. ms sql 带自增列 带外键约束 数据导入导出

    1,生成建表脚本 选中要导的表,点右键-编写表脚本为-create到  ,生成建表脚本 2,建表(在新库),但不建外键关系 不要选中生成外键的那部分代码,只选择建表的代码 3,导数据,用SQL STU ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. C#的泛型的类型参数可以有带参数的构造函数的约束方式吗?

    Review后看到标题让我十分羞愧自己语文功底太差,估计...请见谅......我还特地把这句写回开头了...... 问题 前天遇到的一个问题,所以在MSDN发了个问,刚也丰富了下问题,关于泛型的. ...

  6. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  7. Oracle数据库五种约束

    oracle 数据库 数据表的5个约束类型:1.主键约束2.外键约束3.唯一约束4.检查约束5.非空约束 主键约束:用来唯一标示表中的一个列,一个表中的主键约束只能有一个,但是可以在一个主键约束中包含 ...

  8. 【ZH奶酪】如何用Python计算最长公共子序列和最长公共子串

    1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公共子序列(Longest-Common-Subseq ...

  9. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

随机推荐

  1. 【ABAP系列】SAP ABAP 实现FTP的文件上传与下载

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 实现FTP的文 ...

  2. 机器学习实战-Logistics回归

    Logistics回归:实战,有两个特征X0,X1.100个样本,进行Logistics回归 1.导入数据 def load_data_set(): """ 加载数据集 ...

  3. .net core 学习小结之 配置介绍(config)以及热更新

    命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...

  4. Java语言的发展历程

    前言 自1946年2月14日世界上首款计算机ENAC问世,第一代计算机语言“机器语言”便诞生了,它使用的是最原始的穿孔卡片,这种卡片上使用的语言只有专家才能理解,与人类语言差别极大.这种语言本质上是计 ...

  5. kafka连接器

    独立模式 bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.prope ...

  6. 2. Docker部署tomcat, nginx, redis,及docker私有仓库

    1. 部署tomcat 1.1 下载tomcat       docker pull tomcat:7-jre8 1.2 部署容器  docker run -di --name=tomcat -p 8 ...

  7. webstorm 打开后目录结构不完整

    问题: webstorm自动生成的配置文件.idea/modules.xml损坏了,其实是我把这个.idea目录整个删了 解决方法: 1.删除本地目录历史 点击close Project(若是打开多个 ...

  8. Result window is too large, from + size must be less than or equal to: [10000] but was [78440]. See the scroll api for a more efficient way to request large data sets

    {"error":{"root_cause":[{"type":"query_phase_execution_exception& ...

  9. CSS的优先级理解

    样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...

  10. VB里面的字体颜色

    颜色常数颜色常数 值 描述vbBlack &H0 黑色vbRed &HFF 红色vbGreen &HFF00 绿色vbYellow &HFFFF 黄色vbBlue &a ...