zoukankan      html  css  js  c++  java
  • 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[0];
    
        int num_leftSubTree = tree_mid.find(root);
        int num_rightSubTree = tree_mid.length() - num_leftSubTree - 1;
        if ( tree_fro.length() == 1 ){
            cout << root ;
            return;
        }
        if(num_leftSubTree != 0) {
            string leftSubTree_mid = tree_mid.substr( 0, num_leftSubTree );
            string leftSubTree_fro = tree_fro.substr( 1,num_leftSubTree);
            find( leftSubTree_fro , leftSubTree_mid );
        }
        if(num_rightSubTree != 0) {
            string rightSubTree_mid = tree_mid.substr( num_leftSubTree + 1 ,num_rightSubTree );
            string rightSubTree_fro = tree_fro.substr(num_leftSubTree + 1 ,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 0;
    }
    
    /*
    Sample Input
    
    ABC
    BAC
    Sample Output
    
    BCA
    
    */
  • 相关阅读:
    iOS"伪后台"机制下如何保持APP一直运行在后台(转)(实践通过)
    weak和strong
    iOS webview 清除缓存
    iOS隐藏状态栏
    IOS 在一个应用里打开另一个应用 及其 两个应用互相调用
    ios 检查ios设备是否安装了qq,微信等应用
    【干货篇】步步为营,带你轻松掌握jQuery!
    【化繁为简】非前端开发者的福音---CSS的预处理语言 Less&Sass
    JS中有关分支结构、循环结构以及函数应用的一些简单练习
    万物皆对象的JavaScript
  • 原文地址:https://www.cnblogs.com/JeasonIsCoding/p/9937635.html
Copyright © 2011-2022 走看看