Title: Enter a string that prints all the permutations of the characters in the string.
For example, the input string abc, the output by the character A, B, C can be arranged by all the strings
ABC, ACB, BAC, BAC, cab and Cab.
Analysis:
This question is the main test of recursive thinking.
Each character is removed sequentially, and the string of the remaining characters is printed, plus the starting character.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Realize:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ve
Ctor> #include <algorithm> using namespace std;
Vector<string> printallres (String instr) {vector<string> vecres;
Vecres.clear ();
int len = Instr.length ();
if (len = = 1) {vecres.push_back (instr);
return vecres;
if (len <= 0) return vecres;
Vector<char> _tvecdata;
_tvecdata.clear (); for (int i = 0; i < len i++) {Vector<char>::iterator it = find (_tvecdata.begin (), _tvecdata.end ()
, Instr[i]);
if (it!= _tvecdata.end ()) continue;
_tvecdata.push_back (Instr[i]);
String Tstr (1, instr[i]);
Int J;
String str1 = "";
if (i > 0) str1 = instr.substr (0, I);
String str2 = Instr.substr (i+1, len-i-1); STR1 + STR2;
vector<string> vecares = Printallres (STR1);
int k = 0;
while (K < Vecares.size ()) {string str3 = Tstr + vecares[k];
Vecres.push_back (STR3);
K + +;
} return vecres; int main (int argc, char* argv[]) {if (argc >= 2) {vector<string> vecstr = pri
Ntallres (argv[1]);
for (int i = 0; i < vecstr.size (); i + +) cout << vecstr[i].c_str () << ",";
return 0; }
Compile:
g++ Test.cpp-o Test
Run:
./test ABC
Output:
ABC,ACB,BAC,BCA,CAB,CBA,
Add test Cases (@abamon a friend's proposed vulnerabilities, modifications, and added use cases):
./test AaB
Output:
Aab,aba,baa,
Author: csdn Blog hhh3h