Full arrangement and Lexicographic Arrangement

Source: Internet
Author: User

First of all, full sorting is a simple problem, but I have not actually implemented full sorting.

If I think about the full arrangement by myself, such as the full arrangement of "ABCD", this simple and full arrangement will also make me difficult, because I have never considered this problem. After thinking for a while, I can only give the following troublesome algorithms:

// The Void printre (char * STR, int index, char s [], int length) {If (Index = length) printf ("% s \ n ", str); else {bool exsist = false; For (INT I = 0; I <length; I ++) {exsist = false; For (Int J = 0; j <index; j ++) {If (s [I] = STR [J]) {exsist = true; break ;}} if (! Exsist) {STR [Index] = s [I]; printre (STR, index + 1, S, length) ;}}} void printall (char s [], int length) {char * STR = new char [Length + 1]; STR [length] = '\ 0'; printre (STR, 0, S, length ); delete [] STR ;}
Here, a string array STR is used as a stack to implement Recursive Backtracking. Here, you can determine whether the character is used to traverse the elements in the stack and check whether some characters have been used in the previous strings.

In fact, after reading a lot of ideas about algorithms over the past few days, I am stuck in a state where I don't know how to solve the problem. Alas, my mind is broken down.

Here, a simpler way of thinking is to arrange all the letters starting from 1st, exchange with the subsequent letters, and then recursively output all:

Void printalla (char * STR, char * begin) {If ('\ 0' = * begin) printf ("% s \ n", STR ); else {for (char * P = begin; * P! = '\ 0'; P ++) {swap (* P, * begin); printalla (STR, begin + 1); swap (* P, * begin ); // returns the original status .}}}

This is a recursive method.


Non-recursion in general, the string can be fully arranged using lexicographically arranged.

The lexicographic sorting algorithm is very interesting. Remember:

For the string STR in Lexicographic Order, search for the first vertex STR [I] That is less than the right element from the right direction; then, search for a minimum character STR [J] That is greater than STR [I] from the end of string I to exchange STR [I] and STR [J]. and the substring after I is reversed to obtain a new lexicographically ordered result. Then search for the next sort based on the result.
This algorithm is very interesting. I don't know how such exquisite ideas are generated, but I understand the intention of doing so. First, the starting string is the starting state of the sorted Lexicographic Order. Then, at the beginning of each loop, the string is divided into two segments, and the left side is the child segment without dictionary sorting, the right side is a sub-segment that has been sorted by dictionary, and the Lexicographic Order before this sub-segment has been traversed and output. Here we look for the first element smaller than the right side of STR [I], when the Lexicographic Order before this order has been output, and the right field must be sorted from large to small, the Lexicographic Order of the right sub-segment will also be all output, this also indicates that STR [I] has been completely traversed before and including itself. In this case, STR [I] should be arranged with a larger element, this number is the smallest element of STR [J] in the field on the right that is larger than STR [I]. It exchanges STR [I] and STR [J], traverse the new lexicographic orders starting with str [J]. At this time, the right field is reversed because it is previously sorted from large to small, and then sorted from small to large after switching, this is the first Lexicographic Order of the right sub-segment at the start of STR [J. All the lexicographic orders can be output after repetition.


Then write the code in Lexicographic Order:

Void printlexorder (char s [], int length) {int charbarrel [128]; memset (charbarrel, 0,128*4); For (INT I = 0; I <length; I ++) {charbarrel [s [I] ++;} int I, j; I = 0; char * STR = new char [Length + 1]; STR [length] = '\ 0'; For (j = 0; j <128; j ++) {While (charbarrel [J]> 0) {charbarrel [J] --; STR [I ++] = J ;}} int min; // subscript char * stack = new char [length] of the smallest character greater than I; int stacktop = 0; // use a stack to reverse the string printf ("% s \ n", STR); While (I>-1) {J = length-1; I = J-1; while (STR [I]> = STR [J]) {I --; j --; if (I <0) break;} if (I <0) break; // jump out of two-layer loop J = I + 1; min = J; while (j <length) {If (STR [J]> STR [I] & STR [J] <= STR [Min]) {min = J ;}j ++;} J = min; stack [stacktop] = STR [I]; STR [I] = STR [J]; STR [J] = stack [stacktop]; for (j = I + 1; j <length; j ++) stack [stacktop ++] = STR [J]; while (stacktop> 0) STR [++ I] = stack [-- stacktop]; printf ("% s \ n", STR);} Delete [] stack, STR ;}




Full arrangement and Lexicographic Arrangement

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.