ZJU ACM 1004

/** 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;
}
 

阅读更多
想对作者说点什么? 我来说一句

acm zju 额度cn

2009年03月24日 272KB 下载

浙大ACM-ZOJ 1049

ZOJ 1049

lmp890 lmp890

2010-09-03 15:42:00

阅读数:1070

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭