Tree Recovery POJ - 2255
根据树的前序遍历和中序遍历还原后序遍历。
(偷懒用了stl的find)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string s1,s2; 5 int len; 6 void work(int l1,int r1,int l2,int r2) 7 { 8 if(l1==r1) 9 { 10 cout<<s1[l1]; 11 return; 12 } 13 if(l1>r1) return; 14 char ch=s1[l1]; 15 int pos=s2.find(ch,l2); 16 int len2=pos-l2+1; 17 work(l1+1,l1+len2-1,l2,pos-1);//l2+pos-l2+1-1=pos 18 work(l1+len2,r1,pos+1,r2); 19 cout<<ch; 20 } 21 int main() 22 { 23 while(cin>>s1>>s2) 24 { 25 len=s1.length(); 26 work(0,len-1,0,len-1); 27 cout<<endl; 28 } 29 return 0; 30 }