Tree Recovery POJ - 2255

Tree Recovery POJ - 2255

根据树的前序遍历和中序遍历还原后序遍历。

(偷懒用了stl的find)

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 string s1,s2;
 5 int len;
 6 void work(int l1,int r1,int l2,int r2)
 7 {
 8     if(l1==r1)
 9     {
10         cout<<s1[l1];
11         return;
12     }
13     if(l1>r1)    return;
14     char ch=s1[l1];
15     int pos=s2.find(ch,l2);
16     int len2=pos-l2+1;
17     work(l1+1,l1+len2-1,l2,pos-1);//l2+pos-l2+1-1=pos
18     work(l1+len2,r1,pos+1,r2);
19     cout<<ch;
20 }
21 int main()
22 {
23     while(cin>>s1>>s2)
24     {
25         len=s1.length();
26         work(0,len-1,0,len-1);
27         cout<<endl;
28     }
29     return 0;
30 }
时间: 2024-10-10 00:15:53

Tree Recovery POJ - 2255的相关文章

POJ 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

poj 2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12413   Accepted: 7765 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le

poj 2255 Tree Recovery 分治

Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / / B E / \ / \ A

poj2255 Tree Recovery(求后续遍历,二叉树)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2255 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letter

POJ2255 Tree Recovery (先序中序-》后序)

B - Tree Recovery Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2255 Appoint description: System Crawler (2016-05-08) Description Little Valentine liked

poj2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11955   Accepted: 7505 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le

UVa 536 Tree Recovery(先序,中序求后序)

题意  给你二叉树的先序序列和中序序列  求它的后序序列 先序序列的第一个一定是根  中序序列根左边的都属于根的左子树  右边的都属于右子树  递归建树就行了 #include <bits/stdc++.h> using namespace std; typedef struct TNode { char data; TNode *lc, *rc; } node, *BTree; void build(BTree &t, string pre, string in) { t = new

zoj 1944 Tree Recovery (二叉树)

Tree Recovery Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1