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

十分钟AC 贴40行递归代码

Posted by xuchang at 2011-01-12 17:23:11 on Problem 2255 and last updated at 2011-01-12 17:29:13
#include <iostream>
#include <string>
#include <stack>
using namespace std;


void Recur(stack<char> &st,string pre,string in)
{
	if(pre.length()==0)
		return;
	char root=pre[0];
	st.push(root);
	int index=0;
	while(in[index]!=root)
		index++;
	string preleft=pre.substr(1,index);
	string preright=pre.substr(index+1,in.length()-index-1);
	string inleft=in.substr(0,index);
	string inright=in.substr(index+1,in.length()-index-1);
	Recur(st,preright,inright);
	Recur(st,preleft,inleft);
}

int main()
{
	string pre,in;
	stack<char> st;
	while(cin>>pre>>in)
	{
		Recur(st,pre,in);
		while(!st.empty())
		{
			char c=st.top();
			st.pop();
			cout<<c;
		}
		cout<<endl;
	}
	system("pause");
	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