面试:如何找出字符串的字典序全排列的第N种
1.题目
如何找出字符串的字典序全排列的第N种?(字符串全排列的变种)
2.思路
主要想通过这题,介绍一下康托展开式。基于康托展开式可以解决这个问题。
一般的解法:①求出所有全排列 ②按照字典序排个序 ③取第N个
3.康托展开与逆展开
康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩。 康托展开的实质是计算当前排列在所有由小到大全排列中的顺序,因此是可逆的。(引用)
3.1公式
X=a[n]*(n-1)!+a[n-1]*(n-2)!+…+a[i]*(i-1)!+…+a[1]*0!
其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。
a[i]的意义参见举例中的解释部分
3.2举例
例如,3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释:
排列的第一位是3,比3小的数有两个,以这样的数开始的排列有8!个,因此第一项为2*8!
排列的第二位是5,比5小的数有1、2、3、4,由于3已经出现,因此共有3个比5小的数,这样的排列有7!个,因此第二项为3*7!
以此类推,直至0*0!
伪代码实现
① Cantor(A, n) 求一个字符数组的康托值
Cantor(A, n)
for i← to n-
result ← result + Less(A[i]) * F[i]
return result
定义:
- A:待求康托值的字符数组
- n:字符数组长度,如公式中的n
- F:阶乘的结果集,如公式中(n-1)!、i!、2!、1!、0!
- Less:函数,求比自己小的数的个数,如公式中的a[i]的意义
②Less(n, set) 求比自己小的数的个数,公式中a[i]
Less(n, set)
for(m : set )
if m < n
count ← count+
add(set, n)
return n - count -
定义:
- n:待求比自己小的数
- set:存放已经出现过的数
- count:比3小的数有1,2,如果1,2在set中出现了,count就计数这个。
- 返回值:-1的目的是为了得到a[i]
3.3用途
显然,n位(0~n-1)全排列后,其康托展开唯一且最大约为n!,因此可以由更小的空间来储存这些排列。由公式可将X逆推出对应的全排列。
3.4康托展开的逆运算
既然康托展开是一个双射,那么一定可以通过康托展开值求出原排列,即可以求出n的全排列中第x大排列。
如n=5,x=96时:
- 首先用96-1得到95,说明x之前有95个排列.(将此数本身减去!)
- 用95去除4! 得到3余23,说明有3个数比第1位小,所以第一位是4.
- 用23去除3! 得到3余5,说明有3个数比第2位小,所以是4,但是4已出现过,因此是5.
- 用5去除2!得到2余1,类似地,这一位是3.
- 用1去除1!得到1余0,这一位是2.
- 最后一位只能是1.
- 所以这个数是45321.
伪代码实现
①ResolveCantor(A, X, n):给第X种,求该全排列n的字符串
ResolveCantor(A, X, n)
for i← to n-
a ← X div F[i]
b ← X mod F[i]
A[i] ← Solve(a, visit)
X ← b
return A
定义:
- A:存储字符串的结果
- X:字典序全排列的X种(0,1,2,3,...),这个值是康托值
- n:字符数组长度,如康托公式中的n
- F:阶乘的结果集,如公式中(n-1)!、i!、2!、1!、0!
- Solve:函数,求某个输出字符
②Solve(a, visit):求某个输出字符
Solve(a, visit)
while a is visited
a← a+
see a is visited or not
return a +
定义:
- a:康托公式中的a[i]
- visit:boolean数组,visit[a]表示a是否已经出现过了。
- 返回值:+1 为了构建出输出字符
如果用这个算法去求字符串的全排列,时间复杂度是O(n3),优于递归算法的O(n!)。
3.5 Java代码实现
为了实现简单一些,实现部分采用的是int数组。
import java.util.HashSet;
import java.util.Set; public class Cantor { public static final int LEN = 3;
private static int[] f = new int[LEN];
private static Set<Integer> set = new HashSet<Integer>();
private static boolean[] visit = new boolean[LEN]; static {
int re = 1;
for (int i = 1; i < LEN; i++) {
re *= i;
f[LEN - 1 - i] = re;
}
f[LEN - 1] = 1;
for (int i = 0; i < LEN; i++) {
visit[i] = false;
}
System.out.println("F[0]: " + f[0]);
} public static void main(String[] args) {
int[] a = { 2, 1, 3 };
int n = a.length;
int x = cantor(a, n);
String str = "";
for (int i = 0; i < n; i++) {
str += "" + a[i];
}
System.out.println("src String: " + str);
System.out.println("cantor value: " + x);
int[] b = new int[LEN];
resolveCantor(b, x, n);
str = "";
for (int i = 0; i < n; i++) {
str += "" + b[i];
}
System.out.println("resolve cantor str: " + str);
} static int cantor(int[] a, int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result += less(a[i]) * f[i];
}
return result;
} private static int less(int n) {
int count = 0;
for (Integer m : set) {
if (m < n)
count++;
}
set.add(n);
return n - count - 1;
} static int[] resolveCantor(int[] arr, int x, int n) {
int a, b;
for (int i = 0; i < n; i++) {
a = x / f[i];
b = x % f[i];
arr[i] = solve(a);
System.out.println("a: " + a + " b: " + b + " arr[i]: " + arr[i]);
x = b;
}
return arr;
} private static int solve(int a) {
boolean flag = true;
while (flag) {
if (visit[a] == false) {
visit[a] = true;
flag = false;
} else {
a++;
}
}
return a + 1;
}
}
面试:如何找出字符串的字典序全排列的第N种的更多相关文章
- 35-面试:如何找出字符串的字典序全排列的第N种
http://www.cnblogs.com/byrhuangqiang/p/3994499.html
- 剑指Offer 找出字符串中第一个只出现一次的字符
题目描述 找出字符串中第一个只出现一次的字符 如果无此字符 请输出'.' 输入描述: 输入一串字符,由小写字母组成 输出描述: 输出一个字符 输入例子: asdfasdfo 输出例子: o 思路:数组 ...
- 找出字符串中第一个不重复的字符(JavaScript实现)
如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...
- (1) 一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串
/** * 有一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串 例如: String str ="abc", m=2 得到结果是 "ab" &quo ...
- js常会问的问题:找出字符串中出现次数最多的字符。
一.循环obj let testStr = 'asdasddsfdsfadsfdghdadsdfdgdasd'; function getMax(str) { let obj = {}; for(le ...
- C/C+面试题一:找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)
已知字符串"aabbbcddddeeffffghijklmnopqrst"编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2) /********************* ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
随机推荐
- VC++ 响应回车键的2种方法
众所周知,VC++响应回车键经常用的方法是利用 BOOL PreTranslateMessage(MSG* pMsg) 截取回车键消息,如: if (pMsg->message == WM_KE ...
- Linux Bash算数运算方法小结
A= B= 方法1:let(中间无空格) let C=$A+$B 方法2:$[ ] C=$[$A+$B] 方法3:$(()) C=$(($A+$B)) 方法4:expr(中间有空格) C=`expr ...
- Bash中的数组
变量:$VAR或者${VAR} 数组:${VAR[$i]} 打印整个数组:echo ${VAR[@]} 统计数组元素个数:echo ${#VAR[@]} 从文件读入数组(按行读入):VAR=(`cat ...
- apache、mod_jk负载均衡与tomcat集群
最近需要搭建apache和tomcat的集群,实现静态网站直接通过apache访问,动态网站转交给tomcat处理,实现负载均衡和tomcat集群配置. apache安装 wget http://ap ...
- C# 分页
#region 分页 /// <summary> /// 分页 /// </summary> /// <param name="page">当前 ...
- 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签
学习jsp不得不学习jsp标签,一般来说,对于一个jsp开发者,可以理解为jsp页面中出现的java代码越少,对jsp的掌握就越好,而替换掉java代码的重要方式就是使用jsp标签. jsp标签的分 ...
- 嵌入式web server——Goahead移植要点
前言 在嵌入式设备中,在没有液晶显示的情况下,可以使用web来访问设备,查看设备的运行状态以及进行参数设置,类似于路由器设置.网上有很多关于各种web server的优劣的评论,在此不讨论,只是介绍其 ...
- python隐含的特性
本文源自(http://stackoverflow.com/questions/101268/hidden-features-of-python)希望介绍Python非常有用,而比较忽视的Python ...
- LVS高可用集群
高可用LVS 集群构建 在LVS集群当中Director的作用是很关键的,所以我们在生产环境中要保证其高可用. 高可用架构图: 1.通过 piranha搭建LVS高可用性集群 piranha是REDH ...
- mysql 中执行的 sql 注意字段之间的反向引号和单引号
如下的数据表 create table `test`( `id` int(11) not null auto_increment primary key, `user` varchar(100) no ...