zoukankan      html  css  js  c++  java
  • POJ 2255 Tree Recovery

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

    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 }
    自己选的路,跪着也要把它走完------ACM坑
  • 相关阅读:
    case when if
    存储过程 、函数和事务
    poj 2263
    hdu -1874
    poj 2472
    2544 hdu
    模板floyed
    模板Dijkstra
    hdu 2066
    hdu 2544
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6058618.html
Copyright © 2011-2022 走看看