Baidu face question resolution: string arrangement (String)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.