Anagrams by Stack dfs

來源:互聯網
上載者:User

/*
很明顯的深搜的題目,而且很用到了棧的知識,不錯的題目,
  感覺還不是很懂,有點瓶緊的感覺,應該是對棧不是很熟悉的緣故吧,
  感覺好像做過的dfs的題目好像都有特定的格式一樣的,
  好好加油
  */

#include<iostream>
#include<stack>
using namespace std;
char a[1000];
char b[1000];
bool hash[2000];
int lena, lenb;
stack<char> s;

void dfs(int si, int sj, int sk)
{
 int i;
 if(sj == lenb)
 {
  for(i = 0; i < sk; i++)
  /*  @ PE
  if(i != sk - 1)
  {
   if(hash[i])
    cout<<"i"<<" ";
   else
    cout<<"o"<<" ";
  }
  */
  //else
  //{
   if(hash[i])
    cout<<"i"<<" ";
   else
    cout<<"o"<<" ";
  //}
  cout<<endl;
  return ;
 }
 if(si < lena)
 {
  hash[sk] = true;
  s.push(a[si]);
  dfs(si + 1, sj, sk + 1);
  s.pop();
 }
 //if(sj < lenb)
 if(!s.empty() && b[sj] == s.top())
 {
  //if(b[sj] == s.top())
  //{
   char c = s.top();
   s.pop();
   hash[sk] = false;
   dfs(si, sj + 1, sk + 1);
   s.push(c);
  //}
 }
 return;
}
int main()
{

 while(cin>>a>>b)
 {
  lena = strlen(a);
  lenb = strlen(b);
  if(lena != lenb)
  {

   cout<<"["<<endl;
   cout<<"]"<<endl;

  }
  else
  {
   cout<<"["<<endl;
   dfs(0, 0 , 0);
   cout<<"]"<<endl;
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.