找到一个数的所有字典序即字符串的全排列

来源:互联网 发布:淘宝官方买家秀入口 编辑:程序博客网 时间:2024/06/08 10:52

    只有比别人更早、更勤奋地努力,才能尝到成功的滋味

字典序:对于数字123......n的排列,不同排列的先后关系是从左到右逐个比较对应的数字的先后来决定的。例如对于5个数字的排列 1235412345,排列12345在前,排列12354在后。按照这样的规定,5个数字的所有的排列中最前面的是12345,最后面的是 54321

字典排序算法:

1、首先我们对整个序列进行一个快速排序,得到一个升序列;

2、找到排列中最后的一个升序的首位位置i,得到该值为s[i]

3、找到i值右侧比s[i]大的数中的最小值s[j]

4、交换s[i]s[j].

5、将第i+1位到最后的部分翻转。

 

代码:

#include <stdio.h>#include <stdlib.h>//快速排序void QuickSort(int s[],int l,int r){    if(l<r)    {        int t,i,j;        i=l;        j=r;        t=s[l];        while(i<j)        {            while(i<j&&s[j]>=t)                j--;            if(i<j)            {                s[i]=s[j];                i++;            }            while(i<j&&s[i]<t)                i++;            if(i<j)            {                s[j]=s[i];                j--;            }        }        s[i]=t;        QuickSort(s,l,i-1);        QuickSort(s,i+1,r);    }}//求阶乘int Factorial (int N){    int count=1;    while(N>0)    {        count*=N;        N--;    }    return count;}//翻转字符串void Reverse(int s[],int begin,int end){    int tmp;    while (begin<end)    {        tmp=s[begin];        s[begin++]=s[end];        s[end--]=tmp;    }}void Display(int s[],int N){    int i;    for(i=0; i<N; i++)        printf("%4d",s[i]);    printf("\n");}//字典序查找void DictionaryOrder(int s[],int N){    int i,j,k,n,t,min;    n=Factorial(N);   // printf("n=%d\n",n);    while(n>1)    {        for(i=N-2; (i>=0)&&(s[i]>=s[i+1]); i--)  //找到最后一个升序的首位置s[i]            ;        // printf("i:%4d\n",i);        min=32767;        for(j=i+1; j<N; j++)                     //找到最后一个升序首位置右边的最小值s[j]        {            if(s[j]>s[i])            {                if(s[j]<min)                    min=s[j];                k=j;                //  printf("%4d\n",s[j]);            }        }        t=s[k];                           //交换s[i]与s[j]的值        s[k]=s[i];        s[i]=t;        Reverse(s,i+1, N-1);              //对升序首位置后的数进行反转       // printf("%d:  ",n);        Display(s,N);        n--;    }}int main(){    int str[5];    int N,i,count;    while(printf("Enter N="),scanf("%d",&N)!=EOF)    {        printf("Enter %d nums: \n",N);        for(i=0; i<N; i++)            scanf("%d",&str[i]);        QuickSort(str,0,N-1);        printf("All Dictionary order num:\n");        for(i=0; i<N; i++)            printf("%4d",str[i]);        printf("\n");        DictionaryOrder(str,N);    }    return 0;}

结果:

题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请
按字母顺序输出。

代码:

#include<iostream>#include<algorithm>#include<vector>#include<string>using namespace std;void dictionaryHelp(string str,int k,int len,vector<string> &arr){    if(k==len)    {        //cout<<str<<endl;        arr.push_back(str);    }    else    {        for(char *pCh = &str[k]; *pCh!='\0'; pCh++)        {            if(*pCh == str[k]&&pCh != &str[k])   //针对aa这种结果,这个时候只是输出一个aa                continue;            char temp = *pCh;            *pCh = str[k];            str[k] = temp;            dictionaryHelp(str,k+1,len,arr);            temp = *pCh;  //为了防止重复的情况,还需要将元素重新换回来            *pCh = str[k];            str[k] = temp;        }    }}void dictionarySort(string str){    if(str.size()==0)        return;    int len = str.size();    vector<string> arr;    dictionaryHelp(str,0,len,arr);    cout<<"最后结果未排序"<<endl;    for(int i=0; i<arr.size(); i++)        cout<<arr[i]<<endl;    cout<<"最后结果 排序后"<<endl;    sort(arr.begin(), arr.end());  //对得到的结果进行一次排序,不然最后会出现cba,cab这样的顺序。    for(int i=0; i<arr.size(); i++)        cout<<arr[i]<<endl;}int main(){    string s;    cout<<"输入:"<<endl;    cin>>s;    dictionarySort(s);    return 0;}

结果:



0 0
原创粉丝点击