| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
又过了,简单清晰,递归#include <iostream>
#include <string>
using namespace std;
string str1, str2;
void recovery(int Lstr1, int Rstr1, int Lstr2, int Rstr2) {
if (Lstr2 == Rstr2)
{
cout << str2[Lstr2];
return;
}
if (Lstr2 > Rstr2)//这是中止条件
return;
//找到根节点
int i = Lstr2;
while (str1[Lstr1] != str2[i])
i++;
int mov = i - Lstr2-1;
//下面是分为两颗子数
recovery(Lstr1 + 1, Lstr1 + 1 + mov, Lstr2, i - 1);
recovery(Lstr1 + 1 + mov+1, Rstr1, i + 1, Rstr2);
cout << str2[i];
return;
}
int main() {
while (cin >> str1 >> str2) {
recovery(0, str1.size() - 1, 0, str2.size()-1);
cout << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator