面试:如何找出字符串的字典序全排列的第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 ...
随机推荐
- thinking in java 读书笔记 --- overriding private method
一个对象可以被用作它自身的类型或者是它的基类类型.当用作它的基类类型时叫做upcasting,如下所示 BaseClass ref = new DerivedClass() //upcasting ...
- JavaScript异步操作
//伪代码写法,只用来记录结构 function test(){ var dtd=$.Deferred(); if(成功){ dtd.resolve(data1,data2); //传递参数 } el ...
- ado.net 学习小结
连接数据源 Connection对象.Connection对象处于最顶层,是所有数据访问请求的关口.我们通过其暴露的属性进行配置.下面是一段连接字符串的示例. if (string.IsNullOrE ...
- 【转】Web前端开发规范文档
规范目的: 使开发流程更加规范化. 通用规范: TAB键用两个空格代替(WINDOWS下TAB键占四个空格,LINUX下TAB键占八个空格). CSS样式属性或者JAVASCRIPT代码后加“;”方便 ...
- C++ 常见问题
1:保证编译后方法名不被修改: The: extern "C" { function declarations here in h file } will disable C++ ...
- 在fragment中调用SharedPreferences
[o] Activity中调用SharedPreferences的方式: String prefsName = "mysetting"; SharedPreferences pre ...
- orainstRoot.sh到底执行了哪些操作
1 #!/bin/sh 1 #!/bin/sh 2 AWK=/bin/awk 3 CHMOD=/bin/chmod 4 CHGRP=/bin/chgrp ...
- Lucene分页-----SearcherAfter
/** * 分页,SearcherAfter * @param query * @param pageIndex * @param pageSize */ public void searchPage ...
- 实现基于文件存储的Session类
自主实现Session功能的类,基于文件方式存储Session数据,测试基本通过,还比较好玩,实际应用没有意义,只不过是学习Session是如何实现的. 一般基于文件存储Session数据效率不是很高 ...
- 将CString(unicode)转换为char*(ANSI)
1.将CString(unicode)转换为char*(ANSI) CString strServIP; pChat->GetDlgItemText(IDC_IP,strServIP); ] = ...