2013-07-29 26 views
3

我基本上想要創建由三個操作符號組成的字符串(例如:+-*++/+++)。這些字符串中的每一個應推入vector <string> opPermutations 這是我到目前爲止的代碼:如何使用重複字符生成排列

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

被壓入載體(如字符串)的排列是:但

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

我想要什麼是有我的排列存在這樣的:

  • 每個長度應該在三個字符
  • 每個可能的組合,包括一個字符重複超過一次的字符必須存在。

我希望它被組織成這樣:

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

etc... 

我怎樣才能做到這一點?

+1

你嘗試只用了一堆嵌套的'for'循環? – wlyles

+0

這裏有一些很酷的技巧http://stackoverflow.com/questions/2211915/combination-and-permutation-in-c/ – doctorlove

回答

1

使用遞歸來打印您提出的問題。調整它以存儲向量中的置換字符串應該是微不足道的。我不是一個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<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; 
    } 

輸出:生成排列

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

帶重複元素的字符串不可能排列,因爲排列是一個排序。

你可以用wlyles所說的3個嵌套for循環來做到這一點。

編輯補充:

這將打印我認爲你想要的字符串。您可以用opPermutations.push_back(operatorBank[i]+operatorBank[j]+operatorBank[k])替換cout語句以添加到向量中。

#include <iostream> 
#include <string> 

int main(){ 
    std::string operatorBank[4] = {"+","-","*","/"}; 

    for (int i=0; i<4; ++i){ 
    for (int j=0; j<4; ++j){ 
     for (int k=0; k<4; ++k){ 
     std::cout << operatorBank[i] << operatorBank[j] << operatorBank[k] << std::endl; 
     } 
    } 
    } 

    return 0; 
} 

我想也許混淆了術語「排列」。你可以得到相同的字符串作爲集合{"+","+","+","-","-","-","*","*","*","/","/","/"}的3置換,但使用循環似乎更簡單。

+0

next_permutation很好,重複 – doctorlove

+0

請添加你的工作的代碼示例,我會倒轉下來投票。 – Brian

+0

刪除投票。謝謝! – Brian

1

最簡單的方法是集產品..

list<string> setProduct (list<string> a, list<string> b) 
{ 
    list<string> L; 
    list<string>::iterator i, j; 

    for(i = a.begin(); i != a.end(); ++i) 
     for(j = b.begin(); j != b.end(); ++j) 
      L.push_front(*i + *j); 

    return L; 
} 

list<string> permute (list<string> a, int len) 
{ 
    list<string> L; 

    while (len --> 0) L.splice(a.end(), setProduct(L,a)); 

    return L; 
}