如何生成重复字符的排列 [英] How to Generate Permutations With Repeated Characters

查看:157
本文介绍了如何生成重复字符的排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想创建由三个操作符号组成的字符串(例如: + - * ++ / +++ )。这些字符串中的每一个都应该被推入 vector< string> opPermutations
这是我的代码到目前为止:

  //设置操作符的排列

string operatorBank [4] = {+, - ,*,/};

do {

string currentPerm = operatorBank [0] + operatorBank [1] + operatorBank [2] + operatorBank [3];

this - > opPermutations.push_back(currentPerm);

} while(std :: next_permutation(operatorBank,operatorBank + 4));

推送到向量中的置换(作为字符串)是:

  +  -  * / 
+ - / *
+ / * -
+ / - *
- * + /
- * / +
- + * /
- + / *
- / * +
- / + *
/ * + -
/ * - +
/ + * -
/ + - *
/ - * +
/ - + *



我想要的是我的排列如下:




  • 每个字符的长度应为三个字符

  • 每个可能的排列,包括字符重复一次以上的排列必须存在。



我希望它是这样组织的:

  +++ 
---
***
///
/ * /
+ - +
++ *
** /

等...

如何实现?

解决方案

使用递归打印您提出的问题。使它适应存储在向量中的排列字符串应该是微不足道的。我不是一个c ++程序员,所以在C ++中可能有更好的方法。但这里的主要思想是使用递归。

  #include< iostream> 
#include< string>
#include< math.h>
#include< stdio.h>
#include< stdlib.h>
using namespace std;

void displayPermutation(string permutation [],int length){
int i;
for(i = 0; i cout<< permutation [i];
}
cout<< endl
}

void getPermutations(string operatorBank [],int operatorCount,
string permutation [],int permutationLength,int curIndex){
int i;
//停止递归条件
if(curIndex == permutationLength){
displayPermutation(permutation,permutationLength);
}
else {
for(i = 0; i permutation [curIndex] = operatorBank [i]
getPermutations(operatorBank,operatorCount,permutation,
permutationLength,curIndex + 1);
}
}
}

int main()
{
int operatorCount = 4;
int permutationLength = 3;
string operatorBank [] = {+, - ,*,/};
string permutation [] = {,,,}; //空字符串
int curIndex = 0;
getPermutations(operatorBank,operatorCount,permutation,
permutationLength,curIndex);
return 0;
}

输出:

  +++ 
++ -
++ *
++ /
+ - +
+ -
+ - *
+ - /
+ * +
+ * -
+ **
+ * /
+ / + $ b b +/-
+ / *
+ //


等。


I basically want to create strings that consist of three operation symbols (eg: +-* or ++/ or +++). Each one of these strings should be pushed into vector <string> opPermutations This is my code so far:

 // Set up permutations for operators

string operatorBank[4] = {"+","-","*","/"};

 do {

    string currentPerm = operatorBank[0] + operatorBank[1] + operatorBank[2] + operatorBank[3];

    this -> opPermutations.push_back(currentPerm);

} while ( std::next_permutation(operatorBank, operatorBank + 4) );

The permutations that are pushed into the vector (as strings) are:

+-*/                                                                                                                                                                                           
+-/*                                                                                                                                                                                           
+/*-                                                                                                                                                                                           
+/-*                                                                                                                                                                                           
-*+/                                                                                                                                                                                           
-*/+                                                                                                                                                                                           
-+*/                                                                                                                                                                                           
-+/*                                                                                                                                                                                           
-/*+                                                                                                                                                                                           
-/+*                                                                                                                                                                                           
/*+-                                                                                                                                                                                           
/*-+                                                                                                                                                                                           
/+*-                                                                                                                                                                                           
/+-*                                                                                                                                                                                           
/-*+                                                                                                                                                                                           
/-+*  

What I want however is to have my permutations exist like this:

  • Each should be three characters in length
  • Every possible permutation, including the ones in which a character is repeated more than one time, must be present.

I want it to be organized as such:

+++
---
*** 
/// 
/*/
+-+
++*
**/

etc...

How can I achieve this?

解决方案

Using the recursion to print what you asked. Adapting it to store permutation string in vectors should be trivial. I am not a c++ programmer,so there may be better way to do it in C++. but the main idea here is to use recursion.

    #include <iostream>
    #include <string>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;

    void displayPermutation(string permutation[], int length){
        int i;
        for (i=0;i<length;i++){
            cout<<permutation[i];
        }
        cout << endl;
    }

    void getPermutations(string operatorBank[], int operatorCount, 
            string permutation[],int permutationLength, int curIndex){
        int i;
        //stop recursion condition
        if(curIndex == permutationLength){
            displayPermutation(permutation,permutationLength);
        }
        else{
            for(i = 0; i < operatorCount; i++){
                permutation[curIndex] = operatorBank[i];
                getPermutations(operatorBank,operatorCount,permutation,
                    permutationLength,curIndex+1);
            }
        }
    }

    int main ()
   {
       int operatorCount = 4;
       int permutationLength = 3;
       string operatorBank[] = {"+","-","*","/"};
       string permutation[] = {"","","",""}; //empty string
       int curIndex = 0;
       getPermutations(operatorBank,operatorCount,permutation,
                                   permutationLength,curIndex);
       return 0;
   }

output:

   +++
   ++-
   ++*
   ++/
   +-+
   +--
   +-*
   +-/
   +*+
   +*-
   +**
   +*/
   +/+
   +/-
   +/*
   +//
   .
   .
  and so on.

这篇关于如何生成重复字符的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆