这个题是给树的前序和中序,输出后序。

做法是根据前序找根,根据根在中序中找中序的左右子树,根据左右子树长度找前序的左右子树,依此递归。

做过之后感觉还是比较基础的,废话不多说,上题上代码。

Bob will get a bag as gift from Alice, but Alice don't wanna Bob get the bag without did anything, so she put the bag into a safe box... Alice will give two hints about password to Bob. One is the preorder traversal(root, left subtree, right subtree) of a binary tree, another is the inorder traversal(left subtree, root, right subtree) of the same tree. The password is the postorder traversal(left subtree, right subtree, root) of the tree.
For example:

The tree only has three nodes A, B and C.
Its preorder traversal is ABC, inorder traversal is BAC, and postorder traversal is BCA.

Input

There are several test cases in the input file, Each test case contains two line. Preorder traversal and Inorder traversal.(Each line's length won't longer than 26, and only contain upper letter)

Output

For each test case, output the password Bob need.

Sample Input

ABC
BAC

Sample Output

BCA
/*
* 3988_Password.cpp
* 给出字母的前序和中序,求后序
* Created on: 2018年11月8日
* Author: Jeason
*/ #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define MAX 100
using namespace std; void find(string tree_fro , string tree_mid)
{
char root = tree_fro[]; int num_leftSubTree = tree_mid.find(root);
int num_rightSubTree = tree_mid.length() - num_leftSubTree - ;
if ( tree_fro.length() == ){
cout << root ;
return;
}
if(num_leftSubTree != ) {
string leftSubTree_mid = tree_mid.substr( , num_leftSubTree );
string leftSubTree_fro = tree_fro.substr( ,num_leftSubTree);
find( leftSubTree_fro , leftSubTree_mid );
}
if(num_rightSubTree != ) {
string rightSubTree_mid = tree_mid.substr( num_leftSubTree + ,num_rightSubTree );
string rightSubTree_fro = tree_fro.substr(num_leftSubTree + ,num_rightSubTree );
find( rightSubTree_fro , rightSubTree_mid );
} cout << root;
return;
} int main()
{
string tree_fro;
string tree_mid;
while(cin >> tree_fro){
cin >> tree_mid;
find(tree_fro , tree_mid);
cout << endl;
} return ;
} /*
Sample Input ABC
BAC
Sample Output BCA */

Tju_Oj_3988Password的更多相关文章

随机推荐

  1. 06-docker组件如何协作

    容器启动过程如下: Docker 客户端执行 docker run 命令. Docker daemon 发现本地没有 httpd 镜像. daemon 从 Docker Hub 下载镜像. 下载完成, ...

  2. dijkstra算法计算最短路径和并输出最短路径

    void dijisitela(int d, int m1) { ], book[], path[], u, v, min; l = ; ; i < n1; i++) { dis[i] = w[ ...

  3. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

  4. 20135202闫佳歆--week3 跟踪分析Linux内核的启动过程--实验及总结

    实验三:跟踪分析Linux内核的启动过程 一.调试步骤如下: 使用gdb跟踪调试内核 qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd r ...

  5. Linux内核分析——第六周学习笔记20135308

    第六周 进程的描述和进程的创建 一.进程描述符task_struct数据结构 1.操作系统三大功能 进程管理 内存管理 文件系统 2.进程控制块PCB——task_struct 也叫进程描述符,为了管 ...

  6. jieba分词学习

    具体项目在githut里面: 应用jieba库分词 1)利用jieba分词来统计词频: 对应文本为我们队伍的介绍:jianjie.txt: 项目名称:碎片 项目描述:制作一个网站,拾起日常碎片,记录生 ...

  7. Team抢救最后一下

    Team抢救最后一下 Task1:Team抢救的总结 秦玉:       非常荣幸能以团队的方式获得这次的小黄衫,这件衣服的意义不仅是这门课的奖励,也会是我们整个抢救团队的一个见证和回忆~       ...

  8. Alpha冲刺测试

    项目Alpha冲刺(团队) Alpha冲刺测试 姓名 学号 博客链接 何守成 031602408 http://www.cnblogs.com/heshoucheng/ 黄锦峰 031602411 h ...

  9. OneZero第二周第四次站立会议(2016.3.31)

    会议时间:2016年3月30日  13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容或分配下一步任务. 会议内容: 1.前端,夏.张 ...

  10. day8——ajax传参到action(Struts2)

    第一种:url+?+参数 jsp中: $(function(){ $("[name='delemp']").click(function(){ $this = $(this); $ ...