[CF49E]Common ancestor
[CF49E]Common ancestor
题目大意:
有两个由小写字母构成的字符串\(S\)和\(T(|S|,|T|\le50)\)。另有\(n(n\le50)\)个形如\(a\to bc\)的信息,表示可以将字符\(a\)替换为\(bc\)。定义两个字符串\(s,T\)的祖先\(R\)为能够通过若干次替换,使得其既可以变为\(S\),又可以变为\(T\)的字符串。求\(|R|\)的最小值。
思路:
首先分别预处理\(S,T\)中每一段是否可以通过单个字符转化过来,如果能,可以从哪些字符开始转化。然后枚举\(S,T\)匹配的位置和最后一次匹配到的段,如果最后匹配到的段都可以表示成一个相同的字母,那么就可以转移。
时间复杂度\(\mathcal O(l^4+26^2l^3)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<cstring>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline char getalpha() {
register char ch;
while(!isalpha(ch=getchar()));
return ch;
}
const int N=52,S=26;
char s[N],t[N];
int map[S][S],f[2][N][N],g[N][N];
inline int idx(const char &ch) {
return ch-'a';
}
inline void solve(const char s[],const int &n,int f[N][N]) {
for(register int i=1;i<=n;i++) {
f[i][i]|=1<<idx(s[i]);
}
for(register int i=1;i<=n;i++) {
for(register int j=i-1;j;j--) {
for(register int k=j;k<i;k++) {
for(register int a=0;a<26;a++) {
if(!(f[j][k]>>a&1)) continue;
for(register int b=0;b<26;b++) {
if(!(f[k+1][i]>>b&1)) continue;
f[j][i]|=map[a][b];
}
}
}
}
}
}
int main() {
scanf("%s%s",&s[1],&t[1]);
for(register int i=getint();i;i--) {
const char a=getalpha(),b=getalpha(),c=getalpha();
map[idx(b)][idx(c)]|=1<<idx(a);
}
const int n=strlen(&s[1]),m=strlen(&t[1]);
solve(s,n,f[0]);
solve(t,m,f[1]);
for(register int i=0;i<=n;i++) {
for(register int j=0;j<=m;j++) {
g[i][j]=INT_MAX;
}
}
g[0][0]=0;
for(register int i=1;i<=n;i++) {
for(register int j=1;j<=m;j++) {
for(register int k=1;k<=i;k++) {
for(register int l=1;l<=j;l++) {
if(!(f[0][k][i]&f[1][l][j])) continue;
if(g[k-1][l-1]==INT_MAX) continue;
g[i][j]=std::min(g[i][j],g[k-1][l-1]+1);
}
}
}
}
printf("%d\n",g[n][m]==INT_MAX?-1:g[n][m]);
return 0;
}
[CF49E]Common ancestor的更多相关文章
- CF49E Common ancestor(dp+dp+dp)
纪念卡常把自己卡死的一次自闭模拟赛 QWQ 一开始看这个题,以为是个图论,仔细一想,貌似可以直接dp啊. 首先,因为规则只有从两个变为1个,貌似可以用类似区间\(dp\)的方式来\(check\)一段 ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- swagger2常用注解说明
说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...
- Python函数之内置函数
截止导Python 3.6 目前内置函数有68个 以下是对这些内置函数的分类 一:作用域相关 以字典的形式返回作用域中的名字 locals # 返回本地作用域的所有名字 globals # 返回全局作 ...
- es6 2017
http://es6.ruanyifeng.com/ 18.async 20.Decorator 属于ES2017, nodejs 未实现 21.Module 中的 import export n ...
- Java集合源码学习(三)LinkedList
前面学习了ArrayList的源码,数组是顺序存储结构,存储区间是连续的,占用内存严重,故空间复杂度很大.但数组的二分查找时间复杂度小,为O(1),数组的特点是寻址容易,插入和删除困难.今天学习另外的 ...
- (3).NET CORE微服务 Micro-Service ---- Consul服务治理
Consul是注册中心,服务提供者.服务提供者.服务消费者等都要注册到Consul中,这样就可以实现服务提供者.服务消费者的隔离. 除了Consul之外,还有Eureka.Zookeeper等类似软件 ...
- nginx的with-http_sub_module模块使用之替换字符串
一.介绍 该ngx_http_sub_module模块是一个过滤器,通过将一个指定的字符串替换为另一个字符串来修改响应.该模块不是默认生成的,它应该使用--with-http_sub_module 配 ...
- ip访问网站和localhost访问网站中top使用
对于相对定位,使用margin-top不用简单使用top. top在localhost中能正常显示,在ip访问时会出现多余空白. margin-top不管是localhost中还是ip中都能正常显示.
- ArcGIS 卷帘效果
一直没注意ArcGIS自带了卷帘功能,使用方法:调出Effects工具条,里面就有卷帘工具. AE开发参考: http://bbs.esrichina-bj.cn/esri/viewthread.ph ...
- POJ3041 Asteroids 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...
- JavaSE| 泛型
泛型 泛型:对后续所有操作的类型做约束,对后续操作起作用,对之前的不起作用: 对类型进行约束: 父 ----> 子,从范围上,父范围小,子范围大:把范围小的给范围大的, JDK1.5改写了集合 ...