您的位置:首页 > 其它

ZOJ Problem Set - 1004 Anagrams by Stack

2011-10-18 16:30 351 查看
/*
以下是一段很烂很差的代码,简单的DFS 却 坑死人。对栈的处理不够熟悉。
比起班上某位女牛的代码 相差太大了。
回溯你妹的!回溯的东西都是他妈的参数。
用数来记录比用一开始做的string做的容易理解的多。
对某些恶心的片段那也是为了AC被迫写的!
龙哥 我对自己无语了!
加大对搜索的练习!
*/

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: