您的位置:首页 > 其它

字符数组全排列组合算法汇总

2017-08-23 20:41 344 查看

字符数组全排列

1、递归全排列

public static void Permutation(char[] s, int from, int to) {
if(to<=1)
return;

if(from == to){
System.out.println(s);
}else{
for(int i=from;i<=to;i++){
swap(s,i,from);
Permutation(s,from+1,to);
swap(s,from,i);
}
}
}

public static void swap(char[] s, int i, int j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}


2、去重递归全排列

#include<stdio.h>
#include<string>
//交换两个字符
void Swap(char *a ,char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
//在 str 数组中,[start,end) 中是否有与 str[end] 元素相同的
bool IsSwap(char* str,int start,int end)
{
for(;start<end;start++)
{
if(str[start] == str[end])
return false;
}
return true;
}
//递归去重全排列,start 为全排列开始的下标, length 为str数组的长度
void AllRange2(char* str,int start,int length)
{
if(start == length-1)
{
printf("%s\n",str);
}
else
{
for(int i=start;i<=length-1;i++)
{
if(IsSwap(str,start,i))
{
Swap(&str[start],&str[i]);
AllRange2(str,start+1,length);
Swap(&str[start],&str[i]);
}
}
}
}
void Permutation(char* str)
{
if(str == NULL)
return;
AllRange2(str,0,strlen(str));
}
void main()
{
char str[] = "abb";
Permutation(str);
}


3、Permutation 字典序

如何得到346987521的下一个

从尾部往前找第一个P(i-1) < P(i)的位置 (找到第一个下降的点)

3 4 6 <- 9 <- 8 <- 7 <- 5 <- 2 <- 1

最终找到6是第一个变小的数字,记录下6的位置i-1

从i位置往后找到最后一个大于6的数

3 4 6 -> 9 -> 8 -> 7 5 2 1

最终找到7的位置,记录位置为m

交换位置i-1和m的值

3 4 7 9 8 6 5 2 1

倒序i位置后的所有数据

3 4 7 1 2 5 6 8 9

则347125689为346987521的下一个排列

代码:

private static void PermutationList()
{
int fromIndex, endIndex, changeIndex;
Sort(0, length - 1);
do
{
// 输出一种全排列
Output();
fromIndex = endIndex = length - 1;
// 向前查找第一个变小的元素
while (fromIndex > 0 && words[fromIndex] < words[fromIndex - 1])
--fromIndex;
changeIndex = fromIndex;
if (fromIndex == 0) break;
// 向后查找最后一个大于words[fromIndex-1]的元素
while (changeIndex + 1 < length && words[changeIndex + 1] > words[fromIndex - 1])
++changeIndex;
Swap(fromIndex - 1, changeIndex);   // 交换两个值
InvertArray(fromIndex, endIndex);   // 对后面的所有值进行反向处理
} while (true);
}


字符数组全组合

1、递归

字符串的组合:

给一个字符串,比如ABC, 把所有的组合,即:A, B, C, AB, AC, BC, ABC, 都找出来。

解题思路:

假设我们想在长度为n的字符串中求m个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:一是把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选取m-1个字符;二是不把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选择m个字符。这两种选择都很容易用递归实现。

方法1:



public static void combiantion(char chs[]){
if(chs.length == 0) return ;

Stack<Character> stack = new Stack<Character>();
for(int i = 1; i <= chs.length; i++){
combine(chs, 0, i, stack);
}
}
//从字符数组中第begin个字符开始挑选number个字符加入list中
public static void combine(char []chs, int begin, int number, Stack<Character> stack){
if(number == 0){
System.out.println(stack.toString());
return ;
}
if(begin == chs.length){
return;
}
stack.push(chs[begin]);
combine(chs, begin + 1, number - 1, stack);
stack.pop();
combine(chs, begin + 1, number, stack);
}


方法2:

public static void combine2(char []chs, int begin, List<String> list){
if(begin == chs.length){
return;
}
combine2(chs,begin+1,list);
for(int i = list.size()-1;i>=0;i--){
list.add(chs[begin]+list.get(i));
}
list.add(chs[begin]+"");
}


2、移位

输入三个字符 a、b、c,则它们的组合有a b c ab ac bc abc。当然我们还是可以借鉴全排列的思路,利用问题分解的思路,最终用递归解决。不过这里介绍一种比较巧妙的思路 —— 基于位图。

假设原有元素n个,最终的组合结果有2^n - 1. 可以使用2^n - 1个位,1表示取该元素,0表示不取。 所以a表示001,取ab是011。

001,010,011,100,101,110,111。对应输出组合结果为:a,b,ab,c,ac,bc,abc。

因此可以循环 1~2^n-1(字符串长度),然后输出对应代表的组合即可。

public static void Combination(char [] s){
if(s.length == 0){
return;
}
int len = s.length;
int n = 1<<len;//n的二进制为1000
//从1循环到n-1 即 001 到 111
for(int i=1;i<n;i++){
StringBuffer sb = new StringBuffer();
//每个i的二进制 都是 len 个位
for(int j=0;j<len;j++){
if( (i & (1<<j)) != 0) // 判断i的二进制是否包含 (1<<j)
{
sb.append(s[j]);
}
}
System.out.print(sb + " ");
}
}
for(int j=0;j<len;j++){
if( (i & (1<<j)) != 0)
}


参考链接

http://www.cnblogs.com/pmars/p/3458289.html

https://segmentfault.com/a/1190000002710424

http://wuchong.me/blog/2014/07/28/permutation-and-combination-realize/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: