[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. shell设置连接服务器永不超时

    1.打开/etc/ssh/sshd_config vim /etc/ssh/sshd_config   2.设置如下内容: MaxAuthTries 60 MaxSessions 3 ClientAl ...

  2. 微信小程序开发 如何退出当前页面

    默认是在首页 wx.navigateBack({     delta: -1 });     详情参考. https://mp.weixin.qq.com/debug/wxadoc/dev/api/u ...

  3. 滴水穿石-01JAVA和C#的区别

    排名不分先后,想到哪写到哪 1:数组的定义格式不同 java定义: 方式1: ] ; 方式2: ] ; C#中只有方式1 java有两种,C#只有一种 2:继承的实现关键字不同,同时java中实现接口 ...

  4. Spring Cloud与Spring Boot版本匹配关系

    Spring Cloud是什么? “Spring Cloud provides tools for developers to quickly build some of the common pat ...

  5. python网络爬虫day1

    python爬虫真的很方便,自己不能忽视的问题就是字符编码的问题,一直想腾出时间来看,一直没有时间.明天开始看吧. 今天是学习python爬虫的第一天,从B站上搜到的,可惜可惜. import req ...

  6. MVC AOP解决JsonResult返回json时间格式

    新建JsonNetResult类:JsonResult public class JsonNetResult: JsonResult { public JsonNetResult() { Settin ...

  7. Linux 下压缩与解压.zip和.rar

    )对于.zip linux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序.它们的参数选项很多,可用命令zip -help和unzip -help查看,这里只做简单介绍,举例说明 ...

  8. 真的分治fft

    以前学的分治fft f[i]=sigma(f[i-x]*g[x]),其中g[x]已知 那么我们可以用cdq分治来做(l,mid 对mid+1,t的影响) 而现在的$f[i]=sum(f(i-x)*f( ...

  9. 【BZOJ1417】Pku3156 Interconnect

    题解: 比较简单的一道题 显然我们只需要知道每个联通块的大小就可以了 然后发现x1+xn=30 其中xi<=xi+1的状态只有5000 所以直接记忆化搜索就可以了 emm原来map还可以映射ve ...

  10. Tychonov Theorem

    (Remark: The proof presented in this post is a reorganization and interpretation of that given by Ja ...