Prove safety offer27: lexicographical ordering prints out all permutations of the characters in the string

1 Title Description

  A string input, prints out all permutations of the characters in the string in lexicographic order. For example input string abc, abc print all strings of characters a, b, c can be arranged out, acb, bac, bca, cab and cba.

Enter a description:

  Enter a string of not more than 9 (possibly repeated characters), characters include only lowercase letters.

2 ideas and methods

  Fixing the first character, recursive obtain various combinations of the first string behind; then the first character a character after each exchange, the same character string which is obtained recursively combinations thereof; each recursion is the last bit time, recursive cycle, the second character string is sequentially switched from the first character of each of the sub, and then continue processing substring.

3 C ++ core code

 1 class Solution {
 2 public:
 3     vector<string> result;
 4     vector<string> Permutation(string str) {
 5         if(str.length()==0)
 6             return result;
 7         permutation1(str,0);
 8         sort(result.begin(),result.end());
 9         return result;
10     }
11     void permutation1(string str,int begin){
12         if(begin==str.length())
13         {
14             result.push_back(str);
15             return;
16         }
17         for(int i = begin;str[i]!='\0';i++)
18         {
19             if(i!=begin&&str[begin]==str[i])
20                 continue;
21             swap(str[begin],str[i]);
22             permutation1(str,begin+1);
23             swap(str[begin],str[i]);
24         }
25     }
26 };
View Code

4 C ++ complete code

 1 #include <stdio.h>
 2 #include <vector>
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void swap(char &a, char &b) {
 9     char temp = a;
10     a = b;
11     b = temp;
12 }
13 void permcore(string list, int low, int high, vector<string> & RES) {
 14      IF (Low High && ==
 15          Find (res.begin (), res.end (), List) res.end == ()) {   // deduplication 
16          res.push_back (List) ;
 . 17      }
 18 is      the else {
 . 19          for ( int I = Low; I <= High; I ++) { // each element of the first switching element 
20 is              IF (I == Low || List [I] = List [! Low]) {     // de-emphasis 
21 is                  the swap (List [I], List [Low]);
 22 is                  permcore (List, Low + . 1 , High, RES); // after the exchange, to give the sub-sequence to give the sub-function with perm full permutation sequence 
23                 the swap (List [I], List [Low]); // Finally, the switching element back, restoring, and switching the other element 
24              }
 25          }
 26 is      }
 27  }
 28  
29 Vector < String > Perm ( String STR)
 30  {
 31 is      Vector < String > RES;
 32      IF (! str.empty ())
 33 is          permcore (STR, 0 , str.size () - . 1 , RES);
 34 is      return RES;
 35  }
 36  
37 [  int main ()
38 {
39     vector<string> res;
40     string stdstr = "abb";
41     res = perm(stdstr);
42     for (auto s : res)
43         cout << s << endl;
44     cout << endl;
45 
46     string stdstr2 = "aab";
47     res = perm(stdstr2);
48     for (auto s : res)
49         cout << s << endl;
50     cout << endl;
51 
52     system("pause");
53     return 0;
54 }
View Code

Reference material

https://blog.csdn.net/JarvisKao/article/details/76999473

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11414103.html