[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 ...
随机推荐
- Tomcat配置域名/IP访问及其中遇到的问题注意事项
1.先在tomcat下的conf下找到server.xml文件,用记事本打开后,首先对端口号进行修改,以前一直以为8080是默认的端口号,其实默认的端口号是80 <Connector port= ...
- Vue router拦截 如果用户并未登录直接跳转到登录界面(最简单的cookie演示)
router.beforeEach(function(to,from,next){ console.log('路由拦截') console.log(to.name) console.log(from. ...
- 论文阅读笔记三十九:Accurate Single Stage Detector Using Recurrent Rolling Convolution(RRC CVPR2017)
论文源址:https://arxiv.org/abs/1704.05776 开源代码:https://github.com/xiaohaoChen/rrc_detection 摘要 大多数目标检测及定 ...
- 一脸懵逼学习Hive(数据仓库基础构架)
Hive是什么?其体系结构简介*Hive的安装与管理*HiveQL数据类型,表以及表的操作*HiveQL查询数据***Hive的Java客户端** Hive的自定义函数UDF* 1:什么是Hive(一 ...
- (6).NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- springboot的三种启动方式
一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...
- Windows下80端口被进程System占用的解决方法
最近电脑时不时就发生了80端口被占用的情况,简单百度解决后,当重启电脑的时候又发生被占用的情况.今天非常幸运的是,发生了80端口和8080端口都被占用了情况,忍无可忍决定下定决心解决这个坑爹的问题,经 ...
- Failed to create Accelerated Display. Please check the display hardware and drivers meet the minimum requirements.
ArcGIS Runtime for WPF开发中Map设置了属性UseAcceleratedDisplay="True",报错: Sample: LocalMap Error: ...
- 设置VMware10开机自启动并同时启动虚拟机镜像系统
首先,进入VMware Workstation的安装目录 C:\Program Files (x86)\VMware\VMware Workstation
- UOJ#62. 【UR #5】怎样跑得更快 数论 莫比乌斯反演
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ62.html 题解 太久没更博客了,该拯救我的博客了. $$\sum_{1\leq j \leq n} \ ...