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 |
Re:分享一下自己写的递归,需要的朋友可以参考 208K 0MS C++ 460BIn 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator