Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

又过了,简单清晰,递归

Posted by when at 2016-10-19 21:06:51 on Problem 2255 and last updated at 2016-10-19 21:19:23
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator