Java Solver full Array

Source: Internet
Author: User

One, the problem description

Given a string, the full array of the string is calculated.

For example: "ABC" of the entire arrangement is: ABC, ACB, BAC, BCA, Cab, CBA

Second, realize the idea

It is solved by recursive method. Select one character at a time, and then swap it "several times" to find out all the full permutations of the selected character, and then swap the character "reset" back . At this point, a complete arrangement is completed. The second trip, select the next character, and then the "several times" exchange, find the condition of the selected character, all the whole arrangement, and the character "reset" and then Exchange back. .....

It is similar to: (See the following online explanation:)

Set R={R1,R2,... rn} is the n element to be arranged. Ri=r-{ri}. The full array of elements in collection X is recorded as     Perm (x). RI) Perm (x) indicates that the full permutation of the arrangement R obtained by prefix RI before each permutation of the Perm (x)     is summarized as follows:         when N=1, Perm (r) = (R), where r is the only element in set R;         


The whole arrangement is to exchange the number from the first number to the number behind it, respectively.
The total permutation of the weight is the number of each number from the first number to the non-recurring digital exchange,
In programming, the description is that the number of the first and the number of the J is exchanged, the requirement [I,J] does not have the numbers equal to the number of J.

The code is implemented as follows: Save each arrangement with a linkedlist<string>, if there are duplicate characters in the string, this method will find the number of repetitions, so linkedlist<string> will save the repeating arrangement.

Importjava.util.Collections;Importjava.util.LinkedList; Public classPermutation { Public Static voidallpermutation (String str) {if(str = =NULL|| Str.length () = = 0)            return; //Save all the full permutationsLinkedlist<string> Liststr =NewLinkedlist<string>(); Allpermutation (Str.tochararray (), Liststr,0); Print (LISTSTR);//Print full Array    }            Private Static voidAllpermutation (Char[] C, linkedlist<string> Liststr,intstart) {                if(Start = = C.length-1) Liststr.add (string.valueof (c)); Else{             for(inti = start; I <= c.length-1; i++) {Swap (c, I, start);//equivalent: Fixed first I characterAllpermutation (c, Liststr, start+1);//find out all permutations in this caseSwap (c, start, I);//Reset            }        }    }        Private Static voidSwapChar[] C,intIintj) {        Chartmp; TMP=C[i]; C[i]=C[j]; C[J]=tmp; }        Private Static voidPrint (linkedlist<string>liststr) {Collections.sort (LISTSTR);//make the string output in ' dictionary order '         for(String str:liststr) {System.out.println (str); } System.out.println ("Size:" +liststr.size ()); }        //Hapjin Test     Public Static voidMain (string[] args) {//allpermutation ("Hapjin");Allpermutation ("abc")); }}

If you want the repeating arrangement to be saved only once, there are two ways: ① improved algorithm, do not generate a repeating arrangement ② use HashSet to save the arrangement

How do you create a non-repeating arrangement when duplicate characters appear in a string? the full permutation of the---is to exchange the number from the first number to the non-repeating number that follows it.

The code is implemented as follows: (when there are repeating characters, you can also generate all the correct permutations (permutations do not repeat))

 Public classPermutation { Public Static voidallpermutation (String str) {if(str = =NULL|| Str.length () = = 0)            return; //Save all the full permutationsLinkedlist<string> Liststr =NewLinkedlist<string>(); Allpermutation (Str.tochararray (), Liststr,0); Print (LISTSTR);//Print full Array    }            Private Static voidAllpermutation (Char[] C, linkedlist<string> Liststr,intstart) {        if(Start = = C.length-1) Liststr.add (string.valueof (c));//system.out.println (String.valueof (c)); Else{             for(inti = start; I <= c.length-1; i++)            {                //only when no overlapping words characters exchanged                if(!Isswap (c, start, I)) {Swap (c, I, start);//equivalent: Fixed first I characterAllpermutation (c, Liststr, start+1);//find out all permutations in this caseSwap (c, start, I);//Reset                }            }        }    }        Private Static voidSwapChar[] C,intIintj) {        Chartmp; TMP=C[i]; C[i]=C[j]; C[J]=tmp; }        Private Static voidPrint (linkedlist<string>liststr) {Collections.sort (LISTSTR);//make the string output in ' dictionary order '         for(String str:liststr) {System.out.println (str); } System.out.println ("Size:" +liststr.size ()); }        //whether there is the same character as C[end in [Start,end]    Private Static BooleanIsswap (Char[] C,intStartintend) {         for(inti = start; I < end; i++)        {            if(C[i] = =C[end])return true; }        return false; }        //Hapjin Test     Public Static voidMain (string[] args) {//allpermutation ("Hapjin");Allpermutation ("ABA"); }}

It is important to note that the above implementation saves all the sorting order to the linkedlist<string>. Of course, you can not save the order of arrangement, direct output (Allpermutation method).

if (Start = = C.length-1)    Liststr.add (string.valueof (c)); // Save Arrangement     // System.out.println (string.valueof (c)); // do not save the arrangement,    Direct output

Reference: Full-alignment and full-combination implementations

Some theorems of permutation and combination (II.)

Some theorems of permutation and combination

Original: http://www.cnblogs.com/hapjin/p/5757810.html

Java Solver full Array

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.