# ZOJ Problem Set - 1004 Anagrams by Stack

/*

*/

#include <iostream>
#include <queue>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
char tch[100],cotch[100];
char src[100],aim[100];
stack <char> q;
stack <char> s;

int total;

void dfs(int pos,int tlen,int cotlen,char tmp)// tmp 这里记录的是每次从栈推出的字符，所以要在参数里体现
{

int i,cot;
if(pos == total) {

while(!s.empty()) {tch[tlen++] = s.top();q.push(s.top());cotch[cotlen++] = 'o';s.pop();}
cot = 0;
for(i = 0;i < tlen;i ++) {if(aim[i] == tch[i]) cot++;} if(cot == tlen){
for(i = 0;i < cotlen;i ++) cout<<cotch[i]<<" ";cout<<endl;}
while(!q.empty()){tlen --;cotlen --;s.push(q.top());q.pop();}
return ;
}
else
{
if(pos < total)
{
s.push(src[pos]);
cotch[cotlen ++] = 'i';
dfs(pos+1,tlen,cotlen,tmp);
s.pop();
cotlen --;

}
if(!s.empty())
{
tmp = s.top();
s.pop();cotch[cotlen ++] = 'o';tch[tlen ++] = tmp;
dfs(pos,tlen,cotlen,tmp);
s.push(tmp);
cotlen --; tlen --;
}

}
}
int main()
{
//freopen("a.txt","r",stdin);
while(cin>>src)
{
cin>>aim;
cout<<"["<<endl;
while(!s.empty()) s.pop();
while(!q.empty()) q.pop();
total = strlen(src);
s.push(src[0]);
cotch[0] = 'i';
dfs(1,0,1,'#');
cout<<"]"<<endl;
}
return 0;
}

• 评论

• 下一篇
• 上一篇