简体   繁体   English

按字典顺序打印给定字符串的所有字母组合的算法

[英]Algorithm to print all combination of letters of the given string in lexicographical order

I tried to create the code to generate all possible combination of the given string in the lexicographical order: 我试图创建代码以按字典顺序生成给定字符串的所有可能组合:

The code that I wrote is: 我写的代码是:

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;
 }

Suppose the string is : abcde 假设字符串是:abcde

My code is not generating combinations like: 我的代码未生成如下组合:

abd
abe

The output I am getting for the string abcde are: 我得到的字符串abcde的输出是:

a 
ab
abc 
abcd 
abcde 
ac 
ad
ae 
b 
bc 
bcd 
bcde 
bd 
be 
c 
cd 
cde 
ce 
d 
de 
e

My output does not contains strings like : abd abe 我的输出不包含以下字符串: abd abe

Hope this makes the question clear 希望这可以使问题清楚

How to generate all these combinations using an efficient algorithm 如何使用高效算法生成所有这些组合

This is a simple recursive approach: 这是一种简单的递归方法:

#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;
}

Maybe not the most efficient way of doing it, but a short and simple one. 可能不是最有效的方法,而是一种简短的方法。 Keep in mind, that enumerating all combinations has a complexity of O(2 n ) anyway. 请记住,枚举所有组合的复杂度始终为O(2 n So there exists no efficient algorithm at all. 因此根本不存在有效的算法。

Following may help: 以下内容可能会有所帮助:

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));
}

Live example 现场例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一组字符,如何仅通过重新排列字符来显示具有更高字典顺序的字符串? - Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters? 编写 C++ 程序以乱序打印字符串的字母 - Writing a C++ program to print the letters of a string in a chaotic order 使用 std::string 作为字典顺序的键对 unordered_map 进行排序 - Sorting a unordered_map with a std::string as a key in lexicographical order 按地图排序(Lexicographical Order) - Sorting in Map (Lexicographical Order) 如何检查一个字符串的字母是否在另一个字符串中以给定顺序存在? - How to check whether the letters of a string exist in the given order within another string? 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案? - How should I modify this code to print a diamond pattern using the letters from a given string? 字符串的最小字典值 - Smallest lexicographical value of a string std :: string比较,lexicographical或不 - std::string comparison, lexicographical or not 如何打印给定字符串的所有可能的子字符串? - How do I print all posible substrings of a given string? 从字典顺序中获取范围内的下一个数字(不包含所有字符串) - Get next number from range in lexicographical order (without container of all stringifications)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM