题目:给定元素a,b,a,b,c,c,d,求解出所有的排列。
思路:首先这道题的算法是一个比较经典的算法,它并不是使用分治法思想,也不是使用动态规范的思想,当然更不是贪心的思想。他采用的是穷举法(当然你也可以称之为强力法)!!!之所以强调这个特点,是想提醒自己,对于涉及到数学的概念的题目,往往需要采用强力法,即便使用计算机的思想也不能从根本上降低算法复杂度,除非问题本身存在一个优化的数学解法,比如最大公因子求法,这就是使用辗转相除的方式。
对于本题而言,我们有个数学概念可以改良算法,但是,这个算法并没有本质上降低算法的复杂度,所以,我们还是采用传统的计算机的方法求解全排列。我门借用动态规划思想的备忘录方法进行求解。说到备忘录我想大家就会想到递归,我们就是采用这种递归求解的思想。
代码如下:
#include
#include
#include
using namespace std;
void AllPermutation(vector & vecElementSet, int s, int e, vector & vecAllPermutation)
{
if (s == e) {
vecAllPermutation.push_back(string(vecElementSet.begin(), vecElementSet.end()));
return ;
}
AllPermutation(vecElementSet, s+1, e, vecAllPermutation);
for (int i=s+1; i AllPermutation(vector vecElementSet)
{
string strPermutation;
vector vecAllPermutation;
sort(vecElementSet.begin(), vecElementSet.end());
AllPermutation(vecElementSet, 0, vecElementSet.size(), vecAllPermutation);
return vecAllPermutation;
}
int main(int argc, char ** argv)
{
vector vecElementSet = {
'a', 'b', 'a', 'b', 'c', 'c', 'd'
};
auto vecAllPermutation = AllPermutation(vecElementSet);
for (auto itr=vecAllPermutation.begin(); itr!=vecAllPermutation.end(); ++itr) {
cout << *itr << endl;
}
cout << vecAllPermutation.size() << endl;
// sort(vecAllPermutation.begin(), vecAllPermutation.end());
// vecAllPermutation.erase(unique(vecAllPermutation.begin(), vecAllPermutation.end()), vecAllPermutation.end());
// cout << vecAllPermutation.size() << endl;
//
return 0;
}
该算法对于交换部分是重点,这部分保证了原来重复的元素依然保持连续重复,只有保证了这点,算法才能有效的使用continue的条件。去重复的根本是去掉所有的重复交换!!!(这是参考字典序的算法改进的,该算法的效率不是很高,但是没有更好的办法了)
其实,挺感慨的,学了这么多年的计算机,到头来还是要学习一些数学基本功,但是这些基本功早就还给老师,真的很惭愧,现在连一个高中的排列组合题都不会做了!!!真心不知道自己能在这条路上走多远。
输出:
aabbccd
aabbcdc
aabbdcc
aabcbcd
aabcbdc
aabccbd
aabccdb
aabcdcb
aabcdbc
aabdccb
aabdcbc
aabdbcc
aacbbcd
aacbbdc
aacbcbd
aacbcdb
aacbdcb
aacbdbc
aaccbbd
aaccbdb
aaccdbb
aacdcbb
aacdbcb
aacdbbc
aadccbb
aadcbcb
aadcbbc
aadbccb
aadbcbc
aadbbcc
ababccd
ababcdc
ababdcc
abacbcd
abacbdc
abaccbd
abaccdb
abacdcb
abacdbc
abadccb
abadcbc
abadbcc
abbaccd
abbacdc
abbadcc
abbcacd
abbcadc
abbccad
abbccda
abbcdca
abbcdac
abbdcca
abbdcac
abbdacc
abcbacd
abcbadc
abcbcad
abcbcda
abcbdca
abcbdac
abcabcd
abcabdc
abcacbd
abcacdb
abcadcb
abcadbc
abccabd
abccadb
abccbad
abccbda
abccdba
abccdab
abcdcab
abcdcba
abcdacb
abcdabc
abcdbac
abcdbca
abdccba
abdccab
abdcbca
abdcbac
abdcabc
abdcacb
abdbcca
abdbcac
abdbacc
abdabcc
abdacbc
abdaccb
acbbacd
acbbadc
acbbcad
acbbcda
acbbdca
acbbdac
acbabcd
acbabdc
acbacbd
acbacdb
acbadcb
acbadbc
acbcabd
acbcadb
acbcbad
acbcbda
acbcdba
acbcdab
acbdcab
acbdcba
acbdacb
acbdabc
acbdbac
acbdbca
acabbcd
acabbdc
acabcbd
acabcdb
acabdcb
acabdbc
acacbbd
acacbdb
acacdbb
acadcbb
acadbcb
acadbbc
accabbd
accabdb
accadbb
accbabd
accbadb
accbbad
accbbda
accbdba
accbdab
accdbba
accdbab
accdabb
acdcabb
acdcbab
acdcbba
acdacbb
acdabcb
acdabbc
acdbacb
acdbabc
acdbcab
acdbcba
acdbbca
acdbbac
adccbba
adccbab
adccabb
adcbcba
adcbcab
adcbbca
adcbbac
adcbabc
adcbacb
adcabbc
adcabcb
adcacbb
adbccba
adbccab
adbcbca
adbcbac
adbcabc
adbcacb
adbbcca
adbbcac
adbbacc
adbabcc
adbacbc
adbaccb
adabbcc
adabcbc
adabccb
adacbbc
adacbcb
adaccbb
baabccd
baabcdc
baabdcc
baacbcd
baacbdc
baaccbd
baaccdb
baacdcb
baacdbc
baadccb
baadcbc
baadbcc
babaccd
babacdc
babadcc
babcacd
babcadc
babccad
babccda
babcdca
babcdac
babdcca
babdcac
babdacc
bacbacd
bacbadc
bacbcad
bacbcda
bacbdca
bacbdac
bacabcd
bacabdc
bacacbd
bacacdb
bacadcb
bacadbc
baccabd
baccadb
baccbad
baccbda
baccdba
baccdab
bacdcab
bacdcba
bacdacb
bacdabc
bacdbac
bacdbca
badccba
badccab
badcbca
badcbac
badcabc
badcacb
badbcca
badbcac
badbacc
badabcc
badacbc
badaccb
bbaaccd
bbaacdc
bbaadcc
bbacacd
bbacadc
bbaccad
bbaccda
bbacdca
bbacdac
bbadcca
bbadcac
bbadacc
bbcaacd
bbcaadc
bbcacad
bbcacda
bbcadca
bbcadac
bbccaad
bbccada
bbccdaa
bbcdcaa
bbcdaca
bbcdaac
bbdccaa
bbdcaca
bbdcaac
bbdacca
bbdacac
bbdaacc
bcbaacd
bcbaadc
bcbacad
bcbacda
bcbadca
bcbadac
bcbcaad
bcbcada
bcbcdaa
bcbdcaa
bcbdaca
bcbdaac
bcabacd
bcabadc
bcabcad
bcabcda
bcabdca
bcabdac
bcaabcd
bcaabdc
bcaacbd
bcaacdb
bcaadcb
bcaadbc
bcacabd
bcacadb
bcacbad
bcacbda
bcacdba
bcacdab
bcadcab
bcadcba
bcadacb
bcadabc
bcadbac
bcadbca
bccaabd
bccaadb
bccabad
bccabda
bccadba
bccadab
bccbaad
bccbada
bccbdaa
bccdbaa
bccdaba
bccdaab
bcdcaab
bcdcaba
bcdcbaa
bcdacab
bcdacba
bcdaacb
bcdaabc
bcdabac
bcdabca
bcdbaac
bcdbaca
bcdbcaa
bdccbaa
bdccaba
bdccaab
bdcbcaa
bdcbaca
bdcbaac
bdcabca
bdcabac
bdcacba
bdcacab
bdcaacb
bdcaabc
bdbccaa
bdbcaca
bdbcaac
bdbacca
bdbacac
bdbaacc
bdabcca
bdabcac
bdabacc
bdacbca
bdacbac
bdaccba
bdaccab
bdacacb
bdacabc
bdaaccb
bdaacbc
bdaabcc
cbbaacd
cbbaadc
cbbacad
cbbacda
cbbadca
cbbadac
cbbcaad
cbbcada
cbbcdaa
cbbdcaa
cbbdaca
cbbdaac
cbabacd
cbabadc
cbabcad
cbabcda
cbabdca
cbabdac
cbaabcd
cbaabdc
cbaacbd
cbaacdb
cbaadcb
cbaadbc
cbacabd
cbacadb
cbacbad
cbacbda
cbacdba
cbacdab
cbadcab
cbadcba
cbadacb
cbadabc
cbadbac
cbadbca
cbcaabd
cbcaadb
cbcabad
cbcabda
cbcadba
cbcadab
cbcbaad
cbcbada
cbcbdaa
cbcdbaa
cbcdaba
cbcdaab
cbdcaab
cbdcaba
cbdcbaa
cbdacab
cbdacba
cbdaacb
cbdaabc
cbdabac
cbdabca
cbdbaac
cbdbaca
cbdbcaa
cabbacd
cabbadc
cabbcad
cabbcda
cabbdca
cabbdac
cababcd
cababdc
cabacbd
cabacdb
cabadcb
cabadbc
cabcabd
cabcadb
cabcbad
cabcbda
cabcdba
cabcdab
cabdcab
cabdcba
cabdacb
cabdabc
cabdbac
cabdbca
caabbcd
caabbdc
caabcbd
caabcdb
caabdcb
caabdbc
caacbbd
caacbdb
caacdbb
caadcbb
caadbcb
caadbbc
cacabbd
cacabdb
cacadbb
cacbabd
cacbadb
cacbbad
cacbbda
cacbdba
cacbdab
cacdbba
cacdbab
cacdabb
cadcabb
cadcbab
cadcbba
cadacbb
cadabcb
cadabbc
cadbacb
cadbabc
cadbcab
cadbcba
cadbbca
cadbbac
ccaabbd
ccaabdb
ccaadbb
ccababd
ccabadb
ccabbad
ccabbda
ccabdba
ccabdab
ccadbba
ccadbab
ccadabb
ccbaabd
ccbaadb
ccbabad
ccbabda
ccbadba
ccbadab
ccbbaad
ccbbada
ccbbdaa
ccbdbaa
ccbdaba
ccbdaab
ccdbbaa
ccdbaba
ccdbaab
ccdabba
ccdabab
ccdaabb
cdcaabb
cdcabab
cdcabba
cdcbaab
cdcbaba
cdcbbaa
cdacabb
cdacbab
cdacbba
cdaacbb
cdaabcb
cdaabbc
cdabacb
cdababc
cdabcab
cdabcba
cdabbca
cdabbac
cdbaacb
cdbaabc
cdbacab
cdbacba
cdbabca
cdbabac
cdbcaab
cdbcaba
cdbcbaa
cdbbcaa
cdbbaca
cdbbaac
dccbbaa
dccbaba
dccbaab
dccabba
dccabab
dccaabb
dcbcbaa
dcbcaba
dcbcaab
dcbbcaa
dcbbaca
dcbbaac
dcbabca
dcbabac
dcbacba
dcbacab
dcbaacb
dcbaabc
dcabbca
dcabbac
dcabcba
dcabcab
dcabacb
dcababc
dcacbba
dcacbab
dcacabb
dcaacbb
dcaabcb
dcaabbc
dbccbaa
dbccaba
dbccaab
dbcbcaa
dbcbaca
dbcbaac
dbcabca
dbcabac
dbcacba
dbcacab
dbcaacb
dbcaabc
dbbccaa
dbbcaca
dbbcaac
dbbacca
dbbacac
dbbaacc
dbabcca
dbabcac
dbabacc
dbacbca
dbacbac
dbaccba
dbaccab
dbacacb
dbacabc
dbaaccb
dbaacbc
dbaabcc
dabbcca
dabbcac
dabbacc
dabcbca
dabcbac
dabccba
dabccab
dabcacb
dabcabc
dabaccb
dabacbc
dababcc
dacbbca
dacbbac
dacbcba
dacbcab
dacbacb
dacbabc
daccbba
daccbab
daccabb
dacacbb
dacabcb
dacabbc
daaccbb
daacbcb
daacbbc
daabccb
daabcbc
daabbcc
630