找到一个数的所有字典序即字符串的全排列
来源:互联网 发布:淘宝官方买家秀入口 编辑:程序博客网 时间:2024/06/08 10:52
- 只有比别人更早、更勤奋地努力,才能尝到成功的滋味
字典序:对于数字1、2、3......n的排列,不同排列的先后关系是从左到右逐个比较对应的数字的先后来决定的。例如对于5个数字的排列 12354和12345,排列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
- 找到一个数的所有字典序即字符串的全排列
- 算法练习 - 字符串的全排列(字典序排列)
- 字符串的所有全排列
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。
- 字符串全排列:输入一个字符串,打印字符串元素的所有排列
- 全排列算法的字典序排列
- 输出字符串的所有全排列(递归法和非递归,非递归采用组合数学的字典序)
- 火车进站-即数的全排列问题
- python 火车进站-即数的全排列问题
- 剑指offer_输入一个字符串,按字典序打印出该字符串中字符的所有排列
- 《牛客网剑指offer27题》输入一个字符串,按字典序打印出该字符串中字符的所有排列
- 字符串的排列(字典序)
- 对输入的字符串按字典顺序输出所有的全排列,字符串可以由重复字符
- 全排列字典数
- 一个数n的全排列&&DFS
- 输出一个数的全排列
- 输出一个数n的全排列
- 找出1到n所有不重复的排列,即n的全排列。
- Java多线程实例
- 数据库的最简单实现
- 表空间使用率
- mysql存储过程案例
- 第三章 套接字编程简介(2) - 字节序
- 找到一个数的所有字典序即字符串的全排列
- 使用JS对数组进行排序,设置计时器和计数器
- 解决启动报错:Failed to destroy end point associated with ProtocolHandler["ajp-nio-8009"]
- c++ 十进制、十六进制和BCD的相互转换
- OpenGL中的坐标系
- 关于 Xcode7网络请求的Http 协议改为 Https 协议的程序修改
- C# 自定义特性
- Spring在代码中获取bean的几种方式
- android studio 优秀插件