1004 Anagrams by Stack

来源:互联网 发布:深圳程序员工资2016 编辑:程序博客网 时间:2024/06/10 10:34
// C++Exercise.cpp : 定义控制台应用程序的入口点。//#include<iostream>#include<string>#include<stack>using namespace std;string first, second;int len;void dfs(stack<char> &st,stack<char> data,int n_push,int n_pop){if (n_push == len&&n_pop == len){stack<char> out;stack<char> st1 = st;while (!st1.empty()){char temp = st1.top();st1.pop();out.push(temp);}while (!out.empty()){cout << out.top();out.pop();cout << " ";}cout <<endl;return;}if (n_push < len){st.push('i');data.push(first[n_push]);n_push++;dfs(st,data,n_push,n_pop);data.pop();st.pop();n_push--;}if (!data.empty() &&n_pop<len&& data.top() == second[n_pop]){data.pop();n_pop++;st.push('o');dfs(st, data, n_push, n_pop);st.pop();n_pop--;}}int main(){while (cin >> first >> second){cout << '[' << endl;if (first.size() == second.size()){len = first.size();stack<char> st;stack<char> data;dfs(st,data,0,0); }cout << ']' << endl;}return 0;}