10098 全排列水题

本文介绍了一种解决UVA10098编程题目的方法,使用深度优先搜索(DFS)算法来生成字符串的所有可能排列,并利用集合进行自动去重处理。
 
 


注意可能有重复序列,所以用set先存储下来,最后输出时set会自动去重。。。
/****************************************
* author:crazy_石头
* algorithm:dfs
* date:2013/10/17
* pro:UVA 10098
*****************************************/

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>

using namespace std;

set<string> myset;
set<string>::iterator st;
string ans;

char str[12];
char temp[12];
bool used[12];

int len;

inline void dfs(int cur)
{
    if(cur==len)
    {
        ans=temp;
        myset.insert(ans);
        return ;
    }
    else
    {
        for(int i=0;i<len;i++)
        {
            if(!used[i])
            {
                used[i]=1;
                temp[cur]=str[i];
                dfs(cur+1);
                used[i]=0;
            }
        }
    }
}

int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%s",str);
        myset.clear();
        len=strlen(str);

        sort(str,str+len);

        memset(used,0,sizeof(used));
        memset(temp,0,sizeof(temp));
        dfs(0);

        for(st=myset.begin();st!=myset.end();st++)
        cout<<*st<<endl;
        cout<<endl;
    }
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Default )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值