ZOJ Problem Set - 1004 Anagrams by Stack

/*
以下是一段很烂很差的代码,简单的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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值