您的位置:首页 > 其它

字符串的全排列与拓展

2014-02-28 11:03 357 查看
1、求字符串的全排列的思路是这样的:求字符串的第一个全排列,在此基础上,对于第一位已经确定的情况下求后面所有位的全排列。而从第二位开始求的步骤也和第一位相同,即求第二位的全排列,同时在第二位确定的情况下求后面所有位的全排列,每个位的求法可以依次与它后面位置的字符交换来取得,其实此题是比较简单的,关键是涉及一个经典的算法-回溯法。代码如下:

#include<iostream>
#include <stdio.h>
using namespace std;
//字符串全排列

void allRank(char *result,char *source){
if(result == NULL || source == NULL)
return;
if(*result == '\0'){
cout<<source<<endl;
return;
}
for(char *temp = result;*temp != '\0';temp++){
swap(*result,*temp);
allRank(result+1,source);
swap(*result,*temp);
}

}

int main(){
char test[] = "abc";
allRank(test,test);
cout<<endl;
system("PAUSE");
return 0;
}

2、上面的解法对字符串不重复时有效,如果有相同字符串肯定会得到重复结果,那应该怎么改进呢?

可以根据情况对上面的回溯进行剪枝策略,比如对于字符串abb,第一个字符和第二个字符交换得到bab,由于第二个和第三个字符相同则不必交换,即如果要交换的字符在原来的字符数组中之前有重复字符则不交换。譬如a**b***b(*代表任意字符),在上面的算法中对第一个字符进行交换,先是把第一个b交换得到结果,再交换第二个b时,会检查到有一个重复字符在前面,则避免交换以产生重复。代码如下:

#include<iostream>
#include <stdio.h>
using namespace std;
//字符串不重复全排列
bool CanSwap(char *begin,char *end){
for(char *temp = begin;temp<end;temp++){
if(*temp == *end)
return false;
}
return true;
}
void allRank(char *result,char *source){
if(result == NULL || source == NULL)
return;
if(*result == '\0'){
cout<<source<<endl;
return;
}
for(char *temp = result;*temp != '\0';temp++){
if(CanSwap(result,temp)){
swap(*result,*temp);
allRank(result+1,source);
swap(*result,*temp);
}
}

}

int main(){
char test[] = "abb";
allRank(test,test);
cout<<endl;
system("PAUSE");
return 0;
}

测试结果:abb,bab,bba

3.仍然是字符串abc,怎么得到a,b,c,ab,bc,ac,abc呢?这就涉及到字符串的组合概念了。假设是长度为一的组合,那么可以用回溯法,即选a,输出;不选a,继续,输出b;a,b都不选,输出c。长度为二的组合也类似,代码如下。

#include<stdio.h>
#include<vector>
using namespace std;

void combination_recurve(char *str,int number,vector<char> &result){
if(number == result.size()){
vector<char>::iterator iter = result.begin();
while(iter != result.end()){
printf("%c",*iter);
++iter;
}
printf("\n");
return;
}
if(*str == '\0')
return;
result.push_back(*str);
combination_recurve(str+1,number,result);
result.pop_back();
combination_recurve(str+1,number,result);
}

void combination(char *str){
int length = strlen(str);
if(length <=0)
return;
vector<char> result;
for(int i =1;i<=length;i++){
combination_recurve(str,i,result);
}
}

int main(){
char str[] = "abc";
combination(str);
system("PAUSE");
return 0;
}


4、那么全排列应用呢?就涉及到经典的八皇后问题了,其实都是经典的回溯问题,代码如下。

#include<stdio.h>
#include <stdlib.h>
int column[8];
int g_number = 0;

void swap(int &a,int &b){
int temp = a;
a = b;
b = temp;
}

bool issamediagonal(int sum){
for(int i =0;i<sum;i++)
for(int j =i+1;j<sum;j++){
if( i-j == column[i]-column[j] || j-i == column[i]-column[j])
return false;
}
return true;
}

void eight_queen(int current,int sum){
if(current == sum){
if(issamediagonal(sum)){
++g_number;
for(int i =0;i<sum;i++)
printf("%d",column[i]);
printf("\n");
}
return;
}
for(int i = current;i<sum;i++){
//全排列
swap(column[i],column[current]);
eight_queen(current+1,sum);
swap(column[i],column[current]);
}
}

int main(){
for(int i =0;i<8;i++)
column[i] = i;
eight_queen(0,8);
printf("共有%d种解法",g_number);
system("PAUSE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: