正文

Zju 1004 Anagrams by Stack2007-01-23 20:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/cruxd/22770.html

分享到:

挺早的时候写的。那个时候还不知道C的读入效率比C++高,用的还是vector。

/* source:  zju 1004   */
/* algo:  dfs                */
/* date:  2006/5/17     */
/* author:  Crux.D      */


#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

string str0, str1;
stack<char> cs;
vector<char> cv;
int l;

void prints()
{
 for(int i = 0; i < cv.size(); i ++)
 {
  cout << cv[i] << " ";
 }
 cout << endl;
}

void dfs(int ii, int oi)
{
 if(ii == l && oi == l)
  prints();
 if(ii + 1 <= l)
 {
  cs.push(str0[ii]);
  cv.push_back('i');
  dfs(ii + 1, oi);
  cs.pop();
  cv.pop_back();
 }
 if(oi + 1 <= ii && oi + 1 <= l && cs.top() == str1[oi])
 {
  char tc = cs.top();
  cs.pop();
  cv.push_back('o');
  dfs(ii, oi + 1);
  cs.push(tc);
  cv.pop_back();
 }
}

int main()

 //ifstream cin("in.txt");
 while(cin >> str0 >> str1)
 {
  l = str0.length();
  string t1 = str0, t2 = str1;
  sort(t1.begin(), t1.end()), sort(t2.begin(), t2.end());
  cout << "[" << endl;
  if(t1 == t2)
  {
   dfs(0, 0);
  }
  cout << "]" << endl;
 }
 return 0;
}

阅读(4448) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册