-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.cc
More file actions
79 lines (63 loc) · 1.37 KB
/
Copy pathpermutation.cc
File metadata and controls
79 lines (63 loc) · 1.37 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
void Permutation(char *str);
void Permutation(char *str, char *begin);
void PermutationByOrder(char *str, char *begin);
char str[10];
int main()
{
while(scanf("%s", str) != EOF){
Permutation(str);
}
}
//不保证输出顺序,不能去重
void Permutation(char *str)
{
if(str == NULL || *str == '\0')
return;
char *begin = str;
PermutationByOrder(str, begin);
}
void Permutation(char *str, char *begin)
{
if(*begin == '\0'){
printf("%s\n", str);
return;
}
for(char *pCh = begin; *pCh != '\0'; pCh++){
char temp = *begin;
*begin = *pCh;
*pCh = temp;
Permutation(str, begin+1);
temp = *begin;
*begin = *pCh;
*pCh = temp;
}
}
//以字典序输出,同时去重
void PermutationByOrder(char *str, char *begin)
{
if(*begin == '\0'){
printf("%s\n", str);
return;
}
for(char *pCh = begin; *pCh != '\0'; pCh++){
//保证以字典序输出,必须在for循环里面,??
std :: sort(begin, str + strlen(str));
//去重1
if((pCh - 1 >= begin) && (*pCh == *(pCh - 1)))
continue;
//去重2
if(*pCh == *begin && pCh != begin)
continue;
char temp = *begin;
*begin = *pCh;
*pCh = temp;
PermutationByOrder(str, begin+1);
temp = *begin;
*begin = *pCh;
*pCh = temp;
}
}