简体   繁体   English

如何在C ++中生成以下排列树?

[英]How can I generate the following tree of permutations in C++?

Suppose we have the string ABCD 假设我们有字符串ABCD

I would like to create the following tree: 我想创建以下树:

     ABCD             <------ level 1
 ABC ABD ACD BCD      <------ level 2
AB AC AD BC BD CD     <------ level 3
    A B C D           <------ level 4

And save it inside a vector in the following order: 并按以下顺序将其保存在向量中:

ABCD->ABC->ABD->ACD->BCD->AB->AC->AD->BC->BD->CD->A->B->C->D

So from the starting point, I want to generate the nodes of the next level, store them inside the vector, then generate the nodes of the next level and do the same thing for all the remaining levels 因此,从起点开始,我想生成下一级的节点,将其存储在向量中,然后生成下一级的节点,并对所有其余级别执行相同的操作

I have created the following program to generate level 2 from level 1. 我创建了以下程序以从级别1生成级别2。

void test(int dimensions, vector<string> & nodes, const char* currentNode){

     int i,j;

     for(i=dimensions-1;i>=0;i--){
         char *temp = new char[dimensions];
         int counter = 0;
         for(j=0;j<dimensions;j++){
             if(j!=i){
                temp[counter] = currentNode[j];
                counter++;
             }
          }
          temp[counter] = '\0';
          nodes.push_back(temp);
      }
}

which is called from main: 这是从main调用的:

vector<string> nodes;
int dimension = 4;
nodes.push_back("ABCD");
test(dimension, nodes, "ABCD");

This gives me the following: 这给了我以下内容:

在此处输入图片说明

As you can see the nodes of the level 2 are added successfully, however if I try to apply recursion here, for example for node "ABC" 如您所见,成功添加了2级节点,但是,如果我尝试在此处应用递归,例如对于节点“ ABC”

I would get as a result: 结果是:

AB -> AC -> BC

These will be saved successfully, however if the recursion keeps going, for example for node AB now it will find A -> B 这些将成功保存,但是如果递归继续进行,例如对于节点AB现在它将找到A -> B

so the the resulting order of the nodes saved in the vector won't be how I described in the beginning. 因此,向量中保存的节点的结果顺序将不会是我一开始所描述的。

Instead of 代替

    ABCD->ABC->ABD->ACD->BCD->AB->AC->AD->BC->BD->CD->...

it will be 这将是

ABCD->ABC->ABD->ACD->BCD->AB->AC->A->B->...

Finally, I would like the computation of this tree to be generalized for any number of dimensions. 最后,我希望针对任意数量的维度来概括该树的计算。 For example the initial node could be ABCD or ABCDEFGHIJKLM . 例如,初始节点可以是ABCDABCDEFGHIJKLM

For some reason I believe this is very difficult to do, however I'm not exactly certain about it. 出于某种原因,我认为这很难做到,但是我不确定。 Note that I don't want to use any external libraries for computing the permutations, I need to understand 100% the code in order to proceed with the algorithm that I want to implement. 请注意,我不想使用任何外部库来计算排列,我需要了解100%的代码才能继续执行要实现的算法。

Thank you in advance 先感谢您

As stated in the comments, I don't see how this is remotely related to permutations, but here's the code for what I think you're trying to achieve: 如评论中所述,我看不到它与置换有多远的联系,但是以下是我认为您要实现的代码:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

typedef std::vector<std::string> Layer;

Layer getNextLayer(const Layer &);

int main()
{
   std::vector<Layer> layers;
   layers.push_back(Layer());
   layers[0].push_back("ABCDE");
   while ( layers.back().back().size() > 1 )
   {
       layers.push_back(getNextLayer(layers.back()));
       for ( size_t i = 0; i < layers.back().size(); ++i )
       {
          std::cout << layers.back()[i] << " ";
       }
       std::cout << "\n";
   }
}

Layer getNextLayer(const Layer &layer)
{
   Layer result;
   for ( size_t i = 0; i < layer.size(); ++i )
   {
      const std::string item = layer[i];
      for ( size_t j = 0; j < item.size(); ++j )
      {
         std::string new_item = item;
         new_item.erase(new_item.begin() + j); // erase j^th charachter from item
         result.push_back(new_item);
      }
   }
   std::sort(result.begin(), result.end());
   result.erase(std::unique(result.begin(), result.end()), result.end()); // erase duplicates
   return result;     
}

This creates each layer based on the last one. 这将基于最后一层创建每一层。 To store it all in one vector, you just have to merge all these layers. 要将其全部存储在一个向量中,只需合并所有这些层即可。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM