CF 49E Common ancestor
传送门
模拟赛T1就自闭了(
才不会说我是去刚T2了来着
感觉非常暴力的一个题?
出题人良心开大数据范围 n=100 还是原来的n^4*26算法我也是自闭了
不过貌似跑不到n^4?
真·大力出奇迹
一发记忆化搜索 就是f[l][r][c]表示 l~r化成一个字符c是否可行
然后大力出奇迹!= =+
枚举一下i,j表示第一个字符串的前i个和第二个字符串前j个合成的最短ancestor长度
再枚举从哪个开始合并
判一下就好啦
太真实了。谁敢写这种东西嘛= =+
【本来以为全场A的后来好像也只有4个人A?最后10min30爆搜一次写对还是很开心的= =+】
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 20021225
#define ll long long
using namespace std;
int n,l1,l2;
char s1[110],s2[110],fr[110][2],to[110][3];
int f1[110][110][27],f2[110][110][27];
int id(char ch){return ch-'a'+1;}
int dfs(char *ch,int f[][110][27],char goal,int l,int r)
{
if(r<l) return 0;
if(~f[l][r][id(goal)]) return f[l][r][id(goal)];
if(l==r) return ch[l]==goal;
memset(f[l][r],0,sizeof(f[l][r]));
for(int i=1;i<=n;i++)
for(int k=l;k<r;k++)
if(dfs(ch,f,to[i][0],l,k)&&dfs(ch,f,to[i][1],k+1,r))
{
f[l][r][id(fr[i][0])]=1;break;
}
return f[l][r][id(goal)];
}
int fin[110][110];
int main()
{
scanf("%s%s",s1+1,s2+1);
l1=strlen(s1+1);l2=strlen(s2+1);
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
fr[i][0]=getchar();
while(fr[i][0]<'a'||fr[i][0]>'z') fr[i][0]=getchar();
to[i][0]=getchar();
while(to[i][0]<'a'||to[i][0]>'z') to[i][0]=getchar();
to[i][1]=getchar();
//printf("%s %s",fr[i],to[i]);
}
memset(f1,-1,sizeof(f1));
memset(f2,-1,sizeof(f2));
memset(fin,48,sizeof(fin));
fin[0][0]=0;
for(int i=1;i<=l1;i++)
for(int j=1;j<=l2;j++)
for(int k1=1;k1<=i;k1++)
for(int k2=1;k2<=j;k2++)
{
int pp;
if(fin[k1-1][k2-1]==fin[101][101]) continue;
for(pp=0;pp<26;pp++) if(dfs(s1,f1,pp+'a',k1,i) && dfs(s2,f2,pp+'a',k2,j)) break;
if(pp<26) fin[i][j]=min(fin[i][j],fin[k1-1][k2-1]+1);
}
printf("%d\n",fin[l1][l2]==fin[101][101]?-1:fin[l1][l2]);
return 0;
}
/**
ababa
aba
2
c->ba
c->cc
*/
我的码风怎么越来越奇怪了= =||
CF 49E Common ancestor的更多相关文章
- [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 ...
- leetcode 236. 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 ...
随机推荐
- 如何从word中复制内容到网站后台编辑器中
word图片转存,是指UEditor为了解决用户从word中复制了一篇图文混排的文章粘贴到编辑器之后,word文章中的图片数据无法显示在编辑器中,也无法提交到服务器上的问题而开发的一个操作简便的图片转 ...
- elk+kafka+zookeeper+filebeat安装
ElasticSearch6.0 ElasticSearch6.0安装 #依赖jdk8 rpm -ivh elasticsearch-.rpm vim /etc/elasticsearch/elast ...
- File upload with cropping support using Cropper --jquery file upload
File upload with cropping support using Cropper demo https://tkvw.github.io/jQuery-File-Upload/basic ...
- LightOJ 1342 Aladdin and the Magical Sticks [想法题]
题目链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27050 --------------------------- ...
- day29—JavaScript中DOM的基础知识应用
转行学开发,代码100天——2018-04-14 JavaScript中DOM操作基础知识即对DOM元素进行增删改操作.主要表现与HTML元素的操作,以及对CSS样式的操作.其主要应用知识如下图: 通 ...
- javaSE javaEE javaME的区别、有什么不同?
http://zhidao.baidu.com/link?url=oFEPOmW8BnQ0M0w0krS9DyMA5UCUufgHJWV45r9UQZ-0vp_IOx-Yl-VV0hZQ-vHXGYo ...
- vc code 一个非常不错的插件
https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer 这个是地址,前提下是安装了vc ...
- 利用pwdx查看Linux程序的工作目录
Linux中的pwdx命令,利用进程号作为参数,可以打印出指定进程号的工作目录,帮助我们区分不同的进程. pwdx <pid> [hnyundev@BJ03000036 ~]$ pwd 3 ...
- Python 学习笔记19 安装robot Framework
因为项目组要做自动化测试,本人其实很希望能够使用 MStest + unit + C#来实现. 毕竟产品是基于.net 环境,并且使用C#环境开发的,适用性比较好,一些开发代码可以复用. 但是领导基于 ...
- Neo4j elk Elasticsearch kibana kettle
图形数据库,用于查找犯罪或者啥的很好用:反欺诈 win安装neo4j使用查询https://www.cnblogs.com/rubinorth/p/5853204.html linux下安装neo4j ...