Tju_Oj_3988Password
这个题是给树的前序和中序,输出后序。
做法是根据前序找根,根据根在中序中找中序的左右子树,根据左右子树长度找前序的左右子树,依此递归。
做过之后感觉还是比较基础的,废话不多说,上题上代码。
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的更多相关文章
随机推荐
- vue.js指令总结
1.v-html 用于输出真正html,而不是纯文本. 2.v-text 输出纯文本. <!DOCTYPE html> <html lang="en"> & ...
- js页面实时显示时间
1.通过getMonth()实现获取月份,从0开始计数,需要+1: 2.通过getDay()实现获取星期天数,从0开始,0表示星期日: 3.通过getDate()获取日期. 4.setTimeout( ...
- no-referrer-when-downgrade什么意思
no referrer when downgrade的意思:降级时不推荐. 从一个网站链接到另外一个网站会产生新的http请求,referrer是http请求中表示来源的字段. no-referrer ...
- 一本通1625【例 1】反素数 Antiprime
反素数 Antiprime 题目描述 原题来自:POI 2001 如果一个大于等于 1 的正整数 n,满足所有小于 n 且大于等于 1 的所有正整数的约数个数都小于 n 的约数个数,则 n 是一个反素 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- Jenkins+Jmeter+Ant自动化集成环境搭建
1.搭建环境,安装以下工具 JDK:jdk1.7.0_79 Ant:apache-ant-1.9.7 Jmeter: apache-jmeter-3.0 Jenkins: jenkins-1.651. ...
- Java学习之静态代码块
一.static(在方法区有一块固定的存储区域,由于优先于对象存在,所以可以被所有对象共享) 在定义方法的时候你写main方法的类加static, 写普通类的写方法的时候就不要加static 1.st ...
- Java学习之基本数据类型和引用数据类型区别
JAVA中分为基本数据类型和引用数据类型区别一.基本数据类型: byte:Java中最小的数据类型,在内存中占8位(bit),即1个字节,取值范围-128~127,默认值0 short:短整型,在内存 ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- HDU 4303 Hourai Jeweled 解题报告
HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...