1.翻译参考
http://liucw.blog.51cto.com/6751239/1198026
2.代码参考
http://www.cnblogs.com/devymex/archive/2010/08/04/1792276.html
回溯的方法:先走完二叉树的一路,然后恢复现场,走另一路。
注意出口位置,和条件判断。
STL:vector
(1)http://zh.wikipedia.org/wiki/Vector_(STL)
(2)https://software.intel.com/zh-cn/blogs/2011/08/10/c-vector
反向迭代器rbegin,rend
//反向输入
vector<char> Src(str1.rbegin(), str1.rend());
http://blog.csdn.net/kjing/article/details/6936325
1.顺序?2.结果错误
#include<iostream> #include<string> #include<vector> using namespace std; void GenAnagram(vector<char> &Src, vector<char> &Dest,vector<char> &Order, vector<char> &Stack) { if(!Src.empty()) { Order.push_back('i'); Stack.push_back(Src.back()); Src.pop_back(); GenAnagram(Src, Dest, Order, Stack); Src.push_back(Stack.back()); Stack.pop_back(); Order.pop_back(); } if(!Stack.empty()&&Stack.back()==Dest[Stack.size() + Src.size() - 1]) { Order.push_back('o'); char cTemp =Stack.back(); Stack.pop_back(); GenAnagram(Src, Dest, Order, Stack); Stack.push_back(cTemp); Order.pop_back(); }
//参考代码这块写错了 if (Src.empty() && Stack.empty()) { vector<char>::iterator i = Order.begin(); for (; i != Order.end(); ++i ) { cout << *i << ' '; } cout << endl; } } int main() { for(string str1,str2;cin>>str1>>str2;cout<<']'<<endl) { cout<<'['<<endl; //反向输入 vector<char> Src(str1.rbegin(), str1.rend()); vector<char> Dest(str2.rbegin(), str2.rend()); vector<char> Order, Stack; GenAnagram(Src, Dest, Order, Stack); } return 0; }