![](/img/trans.png)
[英]Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters?
[英]Algorithm to print all combination of letters of the given string in lexicographical order
我试图创建代码以按字典顺序生成给定字符串的所有可能组合:
我写的代码是:
void get(char *n)
{
int l=strlen(n);
sort(n,n+l);
int k=0,m,i,j,z;
while(k<l)
{
m=k;
for(i=k;i<l;i++)
{
for(j=k;j<=i;j++)
cout<<n[j];
cout<<"\n";
}
for(z=m+2;z<l;z++)
cout<<n[m]<<n[z]<<"\n";
k++;
}
}
int main()
{
char n[100];
cin>>n;
get(n);
return 0;
}
假设字符串是:abcde
我的代码未生成如下组合:
abd
abe
我得到的字符串abcde的输出是:
a
ab
abc
abcd
abcde
ac
ad
ae
b
bc
bcd
bcde
bd
be
c
cd
cde
ce
d
de
e
我的输出不包含以下字符串: abd abe
希望这可以使问题清楚
如何使用高效算法生成所有这些组合
这是一种简单的递归方法:
#include <string>
#include <iostream>
using namespace std;
void get( string str, string res ) {
cout << res << endl;
for( int i = 0; i < str.length(); i++ )
get( string(str).erase(i,1), res + str[i] );
}
int main( int argc, char **argv) {
string str = "abcde";
get( str, "" );
return 0;
}
可能不是最有效的方法,而是一种简短的方法。 请记住,枚举所有组合的复杂度始终为O(2 n ) 。 因此根本不存在有效的算法。
以下内容可能会有所帮助:
bool increase(std::vector<bool>& bs)
{
for (std::size_t i = 0; i != bs.size(); ++i) {
bs[i] = !bs[i];
if (bs[i] == true) {
return true;
}
}
return false; // overflow
}
template <typename T>
void PowerSet(const std::vector<T>& v)
{
std::vector<bool> bitset(v.size());
do {
for (std::size_t i = 0; i != v.size(); ++i) {
if (bitset[i]) {
std::cout << v[i] << " ";
}
}
std::cout << std::endl;
} while (increase(bitset));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.