[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 ...
 
随机推荐
- jquery 笔记 点击周围区域子类隐藏,点击子类内部的信息 不隐藏
			
zilei.click(ev){ var e = ev||event; e.stopPropagation(); //dosomething } $(document).click(function( ...
 - js两种写法执行速度比较
			
记录 function test1(){ this.say = function(){} } function test2(){ this.say = function(){} return this ...
 - Java+selenium之WebDriver的cookie,等待等高级操作(五)
			
1. 操作cookie // 增加一个 name = "name",value="value" 的 cookie Cookie cookie = new Coo ...
 - zookeeper都有哪些使用场景
			
分布式协调 这个其实是zk很经典的一个用法,比如,A系统发送个请求到mq,然后B拿到消息消费之后处理了.那A系统如何知道B系统的处理结果? 用zk就可以实现分布式系统之间的协调工作.A系统发送请求之后 ...
 - elemnt UI点击事件失效,得到tab的序号
			
在用element 的tab的时候发现 事件绑定没有作用 看了官网才知到内置有回掉函数 绑定的地方是 <el-tabs></el-tabs> <template> ...
 - SPOJ 1812 LCS2
			
题解: 和上一题差不多 每个点记录前面的到这个点的最大值 初值赋为len[i] 然后注意要用子节点更新父节点 代码: #include <bits/stdc++.h> #define ll ...
 - ArcGIS 10开发迁移策略(待续)
			
1.更改 ESRI.ArcGIS.ADF 程序集 ArcGIS 10 中, ADF 程序集中的功能被分散到不同的程序集中,如果将 ArcGIS 9.3 下 开发的自定义组件迁移到 ArcGIS 10 ...
 - 计划任务_crontab
			
1. crontab原理和使用 Cron 实际上是两个独立的程序.Cron damon, 或者叫做cron ,crond 它是伴随系统一起启动的常驻程序 来检查是否cron 在系统上运行, 用ps 命 ...
 - P2024 [NOI2001]食物链    并查集
			
题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...
 - P1052 过河  线性dp
			
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...