ZJU ACM 1004

来源:互联网 发布:ie浏览器不能登录淘宝 编辑:程序博客网 时间:2024/05/20 22:02

/** ZOJ-1004 Anagrams By Stack
http://acm.zju.edu.cn/show_problem.php?pid=1004
Yimin.Li
**/
#include <iostream>
#include <stack>
#include <vector>
#include <string>

using namespace std;

class AnagramsByStack
{
private:
 vector<char> track;            // "i"¡¢"o"¹ì¼£
 int idxSource, idxDest;        // Ô´×Ö·û´®¡¢Ä¿±ê×Ö·û´® Ë÷Òý
 string strSource, strDest;    // Ô´×Ö·û´®¡¢Ä¿±ê×Ö·û´®
 int strLen;                    // ×Ö·û´®³¤¶È
 
public:
 static stack<char> charStk;    // ²Ù×÷Õ»
 
public:
 void initialize(const string &strSource,
  const string &strDest)
 {
  this->strSource = strSource;
  this->strDest = strDest;
  
  idxSource = 0;
  idxDest = 0;
  
  strLen = strSource.length();
 }
 
 void execute()
 {
  if( idxDest>=strLen )
  {
   vector<char>::iterator iter;
   for(iter=track.begin(); iter!=track.end(); ++iter)
   {
    cout << *iter << ' ';
   }
   cout << endl;
   return ;
  }
  
  if( idxSource<strLen )
  {
   charStk.push(strSource[idxSource++]);
   track.push_back('i');
   execute();
   charStk.pop();
   track.pop_back();
   --idxSource;
  }
  
  if( !charStk.empty() )
  {
   char ch = charStk.top();
   if( strDest[idxDest]==ch )
   {
    charStk.pop();
    track.push_back('o');
    ++idxDest;
    execute();
    --idxDest;
    track.pop_back();
    charStk.push(ch);
   }
  }
  return ;
 }
};

stack<char> AnagramsByStack::charStk;

int main()
{
 string strSource, strDest;
 AnagramsByStack app;
 
 while(cin>>strSource>>strDest)
 {
  cout << "[/n" ;
  if ( strSource.length()==strDest.length() )
  {
   app.initialize(strSource, strDest);
   app.execute();
  }
  cout << "]/n" ;
 }
 
 return 0;
}
 

原创粉丝点击