Sword refers to offer series source code-string arrangement

Source: Internet
Author: User

Topic 1369: String arrangement time limit: 1 seconds Memory limit: 32 Mega Special: No submission: 2432 resolution: 609 Title Description: Enter a string that prints out all the permutations of the characters in the string in dictionary order. For example, enter the string ABC and print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c. Input: Each test case consists of 1 rows. Enter a string that is not more than 9 in length (there may be a repetition of characters), and the characters include only uppercase and lowercase letters. Output: Corresponds to each set of data, outputting all permutations in dictionary order. Sample input: ABCBCA sample output: ABCACBBACBCACABCBAABCACBBACBCACABCBA

#include <stdio.h> #include <stdlib.h> #include <string.h>void bubblesort (char *arr,int begin,int    length), void swap (char *a,char *b), void Permulation (char *arr,int k,int length), int main () {char arr[10];    int length;    int i;        while (gets (arr)) {length = strlen (arr);        Bubblesort (arr,0,length);    Permulation (arr,0,length); } return 0;}    void Permulation (char *arr,int k,int length) {int i;        if (k = = length) {for (i=0;i<length;i++) printf ("%c", Arr[i]);    printf ("\ n");            }else{for (i=k;i<length;i++) {if (k! = i && arr[k] = = Arr[i]) continue;            Swap (&arr[k],&arr[i]);            Bubblesort (arr,k+1,length);            Permulation (arr,k+1,length);        Bubblesort (arr,k+1,length);    }}}void Swap (char *a,char *b) {char C;    c = *b;    *b = *a; *a = C;}    Bubble sort void Bubblesort (char *arr,int begin,int length) {int i,j; for (I=begin;i<length;i++) {for (j=i+1;j<length;j++) {if (Arr[i]>arr[j]) {swap (&arr[i],&arr[j]); }        }    }}

Sword refers to offer series source code-string 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.