[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的更多相关文章

  1. CF49E Common ancestor(dp+dp+dp)

    纪念卡常把自己卡死的一次自闭模拟赛 QWQ 一开始看这个题,以为是个图论,仔细一想,貌似可以直接dp啊. 首先,因为规则只有从两个变为1个,貌似可以用类似区间\(dp\)的方式来\(check\)一段 ...

  2. [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 ...

  3. [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 ...

  4. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  5. [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 ...

  6. 数据结构与算法(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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Python中的函数介绍

    调用函数 python中有很多内置函数,我们可以直接调用,内置函数能直接在官网查看:https://docs.python.org/3/library/functions.html#abs 定义函数 ...

  2. Jumpserver里常用的sudo权限控制模板

    ALL,!/bin/bash,!/bin/tcsh,!/bin/su,!/usr/bin/passwd,!/usr/bin/passwd root,!/bin/vim /etc/sudoers,!/u ...

  3. @ResponseBody 和 @RequestBody 的作用

    先提一嘴 @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 Controller的方法上,作为请求的映射地址. 代码: @RequestMapping(v ...

  4. K3 WISE 开发插件《SQL语句WHERE查询-范围查询/模糊查询》

    0.存储过程开头变量定义 @FBeginDate varchar(10), --单据起始日期 @FEndDate varchar(10), --单据截止日期. @FItemID varchar(50) ...

  5. alpha冲刺6/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:感恩节~ 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 设计了拍照界面和图片上传界面 沟通了前端进度 接下 ...

  6. BZOJ5291/洛谷P4458/LOJ#2512 [Bjoi2018]链上二次求和 线段树

    原文链接http://www.cnblogs.com/zhouzhendong/p/9031130.html 题目传送门 - LOJ#2512 题目传送门 - 洛谷P4458 题目传送门 - BZOJ ...

  7. Scala-Unit7-Scala并发编程模型AKKA

    一.Akka简介 Akka时spark的底层通信框架,Hadoop的底层通信框架时rpc. 并发的程序编写很难,但是Akka解决了spark的这个问题. Akka构建在JVM平台上,是一种高并发.分布 ...

  8. 一、利用Python编写飞机大战游戏-面向对象设计思想

    相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...

  9. P2158 [SDOI2008]仪仗队

    P2158 [SDOI2008]仪仗队图是关于y=x对称的,横纵坐标一定是互质的否则在之前就被扫过了,所以就可以用欧拉函数再*2就完了. #include<iostream> #inclu ...

  10. tensorflow下基于DNN实现实时分辨人脸微表情

    参加学校的国创比赛的时候,我们小组的项目有一部分内容需要用到利用摄像头实现实时检测人脸的表情,因为最近都在看深度学习方面的相关知识,所以就自己动手实现了一下这个小Demo.参考网上的资料,发现大部分是 ...