Full ordering of strings, non-repeating full ordering

Source: Internet
Author: User
Tags repetition

Title: Ask for a full array of strings.

Idea: We can generate a solution tree: Take the string "BCA", at this point, if we deal with the cur position, then we can and from the current position to start and then the position of the interchange character, so as to ensure that the two-tree each layer (the current position) of the results can appear, and then recursively go down.

Package Org;import Java.util.arrays;public class SortMethod {public    static void Create (char a[], int cur, int n) {
   if (cur = = N) {            System.out.println (arrays.tostring (a));            return;        }        for (int i = cur; i < n; i++) {            char tmp = a[cur];            A[cur] = A[i];            A[i] = tmp;            Create (A, cur+1, n);            TMP = A[cur];            A[cur] = A[i];            A[i] = tmp;        }    }    public static void Main (string[] args) {        String a = "bba";        Create (A.tochararray (), 0, A.length ());}    }
Title: Generates a full array without duplicates.

Ideas: And there are some differences, first of all we have to understand that if we can ensure that there is no repetition of the possible, we can put each repetition of the characters in order to mark a subscript, then I just want to ensure that each generated by the entire sequence of repeated characters can maintain the original arrangement, that is, no reverse order, So that we can guarantee that there is no repetition of the sequence generation. Add a for-judge.

Package Org;import Java.util.arrays;public class SortMethod {public static void Create (char a[], int cur, int n) {            if (cur = = N) {System.out.println (arrays.tostring (a));        Return            } for (int i = cur; i < n; i++) {char tmp = a[cur];            A[cur] = A[i];            A[i] = tmp;            Create (A, cur+1, N);            TMP = A[cur];            A[cur] = A[i];        A[i] = tmp; }} public static void Dul (char a[], int cur, int n) {if (cur = = N) {System.out.println (arrays.t            Ostring (a));        Return            } for (int i = cur; i < n; i++) {int vis[] = new INT[30];            for (int j = 0; J <; J + +) Vis[j] = 0;            for (int j = i-1; J >= cur; j--) vis[a[j]-' a '] = 1;            if (vis[a[i]-' a '] = = 1) continue;            char tmp = a[cur];            A[cur] = A[i];            A[i] = tmp;            Dul (A, cur+1, N);      TMP = A[cur];      A[cur] = A[i];        A[i] = tmp;        }} public static void Main (string[] args) {String a = "BBA";        Create (A.tochararray (), 0, A.length ());    Dul (A.tochararray (), 0, A.length ()); }}



Full ordering of strings, non-repeating full ordering

Related Article

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.