File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
chapter-8-recursion-and-Dynamic-Programming Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+
4
+ using namespace std ;
5
+
6
+ void printPerms (string, string = " " );
7
+
8
+ int main ()
9
+ {
10
+ printPerms (" abbc" );
11
+ }
12
+
13
+ void printPerms (string remainder, string prefix)
14
+ {
15
+ long length = remainder.length ();
16
+
17
+ if (!length) cout << prefix << endl;
18
+
19
+ bool dup[128 ];
20
+
21
+ memset (dup, false , sizeof (bool ) * 128 );
22
+
23
+ for (int i = 0 ; i < length; ++i)
24
+ {
25
+ if (dup[remainder.at (i)]) continue ;
26
+
27
+ string str1 = i == 0 ? " " : remainder.substr (0 ,i);
28
+
29
+ string str2 = i == length - 1 ? " " : remainder.substr (i+1 ,length);
30
+
31
+ printPerms (str1 + str2, prefix + remainder.at (i));
32
+
33
+ dup[remainder.at (i)] = true ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments