题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384
题目给出一个长度不超过9的字符串,字符串由0~9这些组成,然后从小到大输出这些字符的全排列,
坑点,重复的串不要输出,可以搞一个map映射一下。
考察知识:递归
AC代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
char a[15];
char ans[15];
int vis[15];
int len;
map<string,int>m;
void dfs(int pos)
{
if(pos == len)
{
ans[len] = '\0';
if(m[ans]==0) ///不能输出重复的。
{
m[ans]=1;
puts(ans);
}
return;
}
for(int i = 0; i < len; i++)
{
if(vis[i]==0)
{
ans[pos] = a[i];
vis[i] = 1;
dfs(pos+1);
vis[i] = 0;
}
}
}
int main()
{
while(~scanf("%s",a))
{
len = strlen(a);
sort(a,a+len);
memset(vis,0,sizeof(vis));
m.clear();
dfs(0);
}
return 0;
}