[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. sass基础—属性嵌套以及跳出嵌套 @at-root

    /*注意:定义的变量若是没有使用则不会编译到css文件中.*//*1)sass的局部变量*/$font:14px;//定义$font:12px !default; //没有default时是重新赋值, ...

  2. jmeter IP欺骗功能

    使用过loadrunner的同学,应该都了解有个IP欺骗功能,jmeter遇到类似需求怎样实现呢? 环境:windows7,jdk1.8,jmeter3.1 使用IP欺骗功能前提是本地有多个可用IP, ...

  3. Java+selenium之WebDriver定位页面元素(二)

    Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法: 单个对象的定位方法,多个对象的定位方法和层级定位 1. 定位单个元素 // 对于元素的属性包含 id 的情况适用,推 ...

  4. javascript 面向对象-面试题实例

    / 从设计到模式 // 设计模式简介 // 设计 // 模式 // 分开 // 从设计到模式 // 23种设计模式 // 创建型 // 工厂模式(工厂方法模式,抽象工厂模式,建造者模式) // 单例模 ...

  5. 【回顾】html属性、标题、段落、文本格式化

    1.HTML 属性 HTML 元素可以设置属性 属性可以在元素中添加附加信息 属性一般描述于开始标签 属性总是以名称/值对的形式出现,比如:name="value". HTML 提 ...

  6. nginx反向代理uwsgi django服务器搭建总结

    1.安装python.django.虚拟环境 参考帖子:安装python django https://blog.csdn.net/a249900679/article/details/5152720 ...

  7. google hacking

    Google是一个强大的搜索引擎:而对于黑客而言,则可能是一款绝佳的黑客工具.正因为google的检索能力强大,黑客可以构造特殊的关键字,使用Google搜索互联网上的相关隐私信息.通过Google, ...

  8. lvs介绍

    1Linux集群及系统扩展的方式概述 集群是有多台服务器组织在一起,一起工作,因为单台服务器的并发响应能力是有限的,响应处理能力也是有限的所有有了集群的出现 在系统扩展有2种方法: 1 向上扩展:是指 ...

  9. jenkins(3): jenkins执行shell命令

    参考: https://www.cnblogs.com/reblue520/p/7146693.html 1. 执行 本地 shell命令或者脚本 是在一个构建中的  bulid 选项卡. 执行本地中 ...

  10. PHP中使用CURL实现GET和POST请求(转载)

    CURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 CURL 库.使用PHP的CURL 库可以简单和有效地去抓网页.你只 ...