传送门

模拟赛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的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. POJ 2502 Subway ( 最短路 && 最短路建图 )

    题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...

  2. Mac终端的Cocoapods创建自己的私有库和公有库

    一,前言 为什么要用Cocopods 通常在开发的过程中,大多时候,我们会处理一类相同的操作,比如对于字符串String的邮箱验证,是否为空,手机号验证,或者一些UIView的动画操作,我们为了避免写 ...

  3. java 简单指令说明

    javac:Java编译器,Java程序的编译工具,用来将Java程序的源文件编译成字节码文件,也就是.class文件.java:Java解释器,解释和执行已经转换成字节码的Java应用程序.jdb: ...

  4. spring boot中@ControllerAdvice的用法

    @ControllerAdvice ,这是一个增强的 Controller.使用这个 Controller ,可以实现三个方面的功能: 全局异常处理 全局数据绑定 全局数据预处理 灵活使用这三个功能, ...

  5. pandas 之dataFrame 数据显示不完全的解决方案

    对于这种省略部分,可以使用set_option进行设置,就可以看到了. # 显示所有行(参数设置为None代表显示所有行,也可以自行设置数字) pd.set_option('display.max_c ...

  6. 架构-SOA:SOA(面向服务的架构)

    ylbtech-架构-SOA:SOA(面向服务的架构) 面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来.接口是采 ...

  7. leetcode 155. 最小栈(c++)

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶元素.get ...

  8. Vagrant 入门 - 同步目录(synced folders)

    原文地址 尽管可以非常轻松的启动一台虚拟机,但很少有人希望通过 SSH 使用基于终端的编辑器来编辑文件.幸运的是,借助 Vagrant 你不需要这样做.通过使用同步目录,Vagrant 会自动同步 g ...

  9. 16/7/11_PHP-图形图像操作

    GD库简介 GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新的图片. PHP除了能进行文本处理以外,通过GD ...

  10. 05 | 基础篇:某个应用的CPU使用率居然达到100%,我该怎么办?

    通过前两节对平均负载和 CPU 上下文切换的学习,我相信你对 CPU 的性能已经有了初步了解.不过我还是想问一下,在学这个专栏前,你最常用什么指标来描述系统的 CPU 性能呢?我想你的答案,可能不是平 ...