码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2255 Tree Recovery

时间:2016-11-13 16:16:23      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:problem   tree   void   stream   nbsp   poj   class   std   color   

主要是递归思想吧,然后找到边界条件,推了好久。

http://poj.org/problem?id=2255

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 string str1, str2;
 6 void recovery(int Lstr1, int Rstr1, int Lstr2, int Rstr2) {
 7     if (Lstr2 == Rstr2)
 8     {
 9         cout << str2[Lstr2];
10         return;
11     }
12     if (Lstr2 > Rstr2)//这是中止条件
13         return;
14     //找到根节点
15     int i = Lstr2;
16     while (str1[Lstr1] != str2[i])
17         i++;
18     int mov = i - Lstr2 - 1;
19     //下面是分为两颗子数
20     recovery(Lstr1 + 1, Lstr1 + 1 + mov, Lstr2, i - 1);
21     recovery(Lstr1 + 1 + mov + 1, Rstr1, i + 1, Rstr2);
22 
23 
24     cout << str2[i];
25     return;
26 }
27 
28 int main() {
29     while (cin >> str1 >> str2) {
30         recovery(0, str1.size() - 1, 0, str2.size() - 1);
31         cout << endl;
32     }
33     return 0;
34 }

 

POJ 2255 Tree Recovery

标签:problem   tree   void   stream   nbsp   poj   class   std   color   

原文地址:http://www.cnblogs.com/IKnowYou0/p/6058618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!