poj 2255 Tree Recovery 树的遍历 简单string用法

来源:互联网 发布:怎么在淘宝买二手表 编辑:程序博客网 时间:2024/05/17 02:36

给定前序和中序遍历,求后续遍历

 

用G++, c++会ce,string用着总感觉各种ce,不大会用,但这题用string真心容易些

 

前序遍历为 中左右  中序遍历为左中右

DBACEGF

ABCDEFG

粉色为左子树 D 为根 递归下去求即可

 

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;string dfs(string s1, string s2){    if( s1.length() == 1) return s1;    if( s1.length() == 0) return "";    int m= s2.find(s1[0]);    return dfs( s1.substr(1, m), s2.substr(0, m)) + dfs( s1.substr(m+1), s2.substr(m+1)) + s1.substr(0, 1);}int main(){//freopen("1.txt", "r", stdin);string str1, str2;    while( cin>>str1>>str2 ){        cout<<dfs(str1, str2)<<endl;    }return 0;}

 

 

 

    string s1;    cin>>s1;                         // abcdefghij    cout<<s1.find('a')<<endl;        // 0    cout<<s1.substr(1, 5)<<endl;     // bcdef    cout<<s1.substr(1)<<endl;        // bcdefghij    cout<<s1+"aaa"<<endl;            // abcdefghijaaa    cout<<s1.length()<<endl;         // 10



 

原创粉丝点击