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

Re:分享一下自己写的递归,需要的朋友可以参考 208K 0MS C++ 460B

Posted by victor0127 at 2011-10-21 16:57:00 on Problem 2255
In Reply To:分享一下自己写的递归,需要的朋友可以参考 208K 0MS C++ 460B Posted by:xiaoye51888 at 2010-10-10 22:57:32
> #include <iostream>
> #include <string>
> 
> using namespace std;
> 
> string f(string s1,string s2)
> {
> 	if(s1.length()==1) return s1;
> 	else if(s1.length()==0) return "";
> 	else
> 	{
> 		size_t m=s1.find(s2[0]);
> 		return f(s1.substr(0,m),s2.substr(1,m))+f(s1.substr(m+1),s2.substr(m+1))+s2[0];
> 	}
> }
> 
> int main()
> {
> 	string s1,s2,s;//s1为preorder traversal  s2为inorder traversal 
> 	while(cin>>s1>>s2)
> 	{
> 		s=f(s2,s1);
> 		cout<<s<<endl;
> 		
> 	}
> 
> 	return 0;
> }
> 
> 递归的思路是拿给的范例自己画了一下,运行范例OK就提交了,结果AC



if(s1.length()==1) return s1; 这一行是多余的 它不是递归结束条件。 对是否为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