Full array of string arrays--arrays

Source: Internet
Author: User

Topic description Narration:

Enter a string that prints out all the permutations of the characters in the string.


Problem Solving Ideas:

Reference July's programming Art series. Use a dictionary to sort. The next dictionary sequence that is currently arranged. The next arrangement is the full arrangement.

The so-called dictionary sequence, that is, given two partial-order sets A and B, (b) and (a′,b′) belong to the Cartesian set AXB. The dictionary order is defined as

(A, B) ≤ (a′,b′) when and only if a < a′ or (a = a′ and b≤b′).

So given a two string. Transmitting the word by character, then the dictionary with the smaller character first appears in small order. Assume that characters are always equal. Shorter string dictionaries are small in order.

For example: ABC < ABCD < Abde < Afab.

Starting point: The smallest permutation of the dictionary order, 1-n, such as 12345
End point: The largest permutation of the dictionary order. N-1, like 54321.
Process: The next arrangement that produces a dictionary order that is just larger than the current arrangement

Next_permutation algorithm

Defined

Ascending: Adjacent two positions AI < Ai+1,ai called the first in ascending order
Step (two find, one swap, one flip)

    1. Find the first position in the last (right) ascending order of the arrangement i,x = AI
    2. Locate the last position of the first I-bit to the right of the arrangement J. y = AJ
    3. Swap x, Y
    4. Flip the section (i+ 1) to the last part
take 21543 For example, so the steps to apply the next_permutation algorithm are as follows:
    • x = 1.
    • y = 3
    • 1 and 3 switching
    • Get 23541
    • Flip 541
    • Get 23145

Reference Code:
<span style= "FONT-SIZE:18PX;" >class solution{public:void calcallpermutaion (char *perm, int from, int. to) {if (to <= 1) return;if (from = = to) {( int i = 0; I <= to; i++) cout << perm[i];cout << Endl;} else{for (int j = from, J <= to; j + +) {swap (perm[j], Perm[from]), Calcallpermutaion (perm, from + 1, to); Swap (Perm[j], per M[from]);}}} BOOL Calcnextpermutation (char *perm, int len) {int i = 0, j = 0;for (i = len-2; (i >= 0) && (perm[i] >= perm[i + 1]); i--); if (I < 0) return false;for (j = len-1; (j>i) && (Perm[j] <= perm[i]); j--); swap (Perm[i], perm[j]); reverse (perm + i + 1, perm + len-1); return true;}; </span>




References: http://taop.marchtea.com/01.06.html

Full array of string arrays--arrays

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.