1384

写的有点憨憨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include <set>
#include <algorithm>
#include <string.h>
#include<queue>
#include<map>
#define ll long long
using namespace std;
map<string,int>mp;
int m, n,cnt=1;
const int N = 10005;
bool vis[N];
char ans[N];
string str;
string temp="";
void (int s,int k)
{
if(s==k)
{
temp="";
for (int i = 0; i <k;i++)
if((int)ans[i]==0)
return ;
for (int i = 0; i <k;i++)
{
temp+=ans[i];
}
if(mp[temp]) return ;
cout <<temp<< endl;
mp[temp]++;
return ;
}
for (int i = 0; i <= str.size();i++)
{
if(vis[i])
continue;
vis[i] = 1;
ans[s] = str[i];

dfs(s + 1, k);
vis[i] = 0;
}
}
int main()
{
cin >> str;
sort(str.begin(), str.end());
//cout<<str<<endl;
dfs(0,str.size());
}

还有一种用函数的写法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[10];
int a[10];
int main(){
int n;
scanf("%s",s);n=strlen(s);
sort(s,s+n);printf("%sn",s);
//for(int i=0;i<n;i++)a[i]=s[i]-'0';
while(next_permutation(s,s+n)){
printf("%sn",s);
}return 0;
}