poj 2255 Tree Recovery 分治
Description
This is an example of one of her creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e5+,M=1e6+,inf=1e9+;
char a[],b[];
char ans[N];
void dfs(char *a,char *b,char *c,int len)
{
if(len<=)
return;
int n=strlen(b),pos=;
for(int i=;i<n;i++)
{
if(b[i]==a[])
{
pos=i;
break;
}
}
int l=pos;
int r=len-pos-;
dfs(a+,b,c,l);
dfs(a+l+,b+l+,c+l,r);
c[len-]=a[];
}
int main()
{
int x,y,z,i,t;
while(~scanf("%s%s",a,b))
{
x=strlen(a);
dfs(a,b,ans,x);
ans[x]=;
cout<<ans<<endl;
}
return ;
}
poj 2255 Tree Recovery 分治的更多相关文章
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; ...
- poj 2255 Tree Recovery(求后序遍历,二叉树)
版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...
- POJ 2255 Tree Recovery 二叉树恢复
一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 只是 ...
- POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...
随机推荐
- Again Array Queries---Lightoj1100(循环暴力)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1100 题意是给你n个数,q个询问,每次求出 a 到 b(从0开始)最小差值: 直接暴力 ...
- SQL Server 排名函数
个函数进行的解释. 以下是对这4个函数的解释: RANK() 返回结果集的分区内每行的排名.行的排名是相关行之前的排名数加一. 假设两个或多个行与一个排名关联,则每一个关联行将得到同样的排名. 比如, ...
- libx264 安卓编译
https://github.com/RoyGuanyu/build-scripts-of-ffmpeg-x264-for-android-ndk/blob/master/ffmpeg/build_a ...
- TensorFlow学习笔记(七)Tesnor Board
为了更好的管理.调试和优化神经网络的训练过程,TensorFlow提供了一个可视化工具TensorBoard.TensorBoard可以有效的展示TensorFlow在运行过程中的计算图..各种指标随 ...
- python3 用requests 保存网页以及BeautifulSoup保存图片,并且在本地可以正常显示文章的内容和图片
用requests 模块做了个简单的爬虫小程序,将博客的一篇文章以及图片保存到本地,文章格式存为'.html'.当文章保存到本地后,图片的连接可能是目标站点的绝对或者相对路径,所以要是想在本地也显示图 ...
- Java并发编程实战4章
第4章主要介绍如何构造线程安全类. 在设计线程安全类的过程中,需要包含以下三个基本要素: 找出构成对象状态的所有变量. 找出约束状态变量的不变性条件. 建立对象状态的并发访问管理策略. 构造线程安全类 ...
- 基于wtforms源码实现自定义form组件
from flask import Flask,Markup,render_template,request,redirect from wtforms.form import Form from w ...
- sql server 复制表从一个数据库到另一个数据库
sql server 复制表从一个数据库到另一个数据库 /*不同服务器数据库之间的数据操作*/ --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQL ...
- 82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 对Spring Ioc 以及DI的精彩理解
转:http://blog.csdn.net/cyjs1988/article/details/50352916 学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注 ...