2014-12-03 51 views
-3

如何爲一組字符生成有序組合?如何在數組中生成一組字符的非重複可能組合?

例如,給定的陣列['a','b','c','d'],寫入用於通過字典順序由長度遞增排序,然後遞增的所有可能組合中的程序 - 換句話說:

ab ac ad bc bd cd abc abd acd bcd abcd 
+0

這聽起來像功課或黑客的挑戰。無論如何,你有什麼嘗試,你有什麼具體問題?另外,你想用什麼編程語言來解決這個問題? – J0e3gan 2014-12-04 06:26:40

+0

它沒有作業或黑客的挑戰。我正在嘗試獲取數組元素的所有組合。您可以使用您選擇的任何語言。 – 2014-12-05 04:24:24

+0

「我正在嘗試獲取數組元素的所有組合」。如果是這樣的元素將是 aa ab ac ad ba bb bc bd ca cb cc cd .... etc等 而不是 ab ac ad bc bd cd abc abd acd bcd abcd – 2014-12-06 12:11:21

回答

0

嘗試這一個。雖然我已經將它寫在控制檯應用程序中,但通過將靜態無效Main(string [] args)更改爲它的工作方法。

static void Main(string[] args) 
{ 
    Dictionary<string, int> s = new Dictionary<string, int>(); 
    Dictionary<string, int> dict = new Dictionary<string, int>(); 
    bool bln = false; 
    string[] str = new string[5] { "a", "b", "c", "d","e"}; 
    int len = str.Length; 
    string lastWord = ""; 
    // a,b,c,d - count=1, aa,ab,ac,ad,ba,bb,bc,bd - count=2 etc 
    int count = 1; 
    foreach(string sa in str) 
    { 
     dict.Add(sa,1); 
    } 

    for(int m=0;m<len;m++) 
    { 
     // Gets last word as eeeee in this example    
     lastWord += str[4]; 
    } 
    for (int i = 0; i < len; i++) 
    { 
     foreach (KeyValuePair<string, int> kvp in dict) 
     { 
       for (int j = 0; j < str.Length; j++) 
       { 
        if (kvp.Value == count) 
        {       
         s.Add(kvp.Key + str[j],count + 1); 
         // If last combination has reached 
         if (s.Keys.Contains(lastWord)) 
          bln = true; 
        } 
       } 
     }     
     foreach (KeyValuePair<string, int> kvp in s) 
     { 
      if (kvp.Value == count + 1) 
      { 
       dict.Add(kvp.Key,kvp.Value); 
      } 
     } 
     if (bln) 
      break; 
     count++; 
     // a,b,c,d - 4 combinations. aa,ab,ac,ad,ba,bb,bc,bd...4*4=16, then 64 etc 
     len = len * str.Length; 
    } 
    dict.Clear(); 
    foreach (KeyValuePair<string, int> kvp in s) 
    { 
      string s1 = SortWord(kvp.Key); 
      if(!dict.Keys.Contains(s1)) 
       dict.Add(s1, kvp.Value);    
    }  
    foreach (KeyValuePair<string, int> kvp in s) 
    { 
     // abdc will get sorted to abcd 
     string s1 = SortWord(kvp.Key); 
     // If aabc then false becz 'a' is repeated two times 
     bool b = IsWordsRepeat(s1); 
     if (dict.Keys.Contains(s1) && !b) 
     {     
       dict.Remove(SortWord(kvp.Key));      
     } 
    } 
    Console.ReadLine(); 
} 

獲取一個布爾狀態檢查排序(即ABDC爲abcd)

static string SortWord(string str) 
{ 
    char[] chars = str.ToArray(); 
    Array.Sort(chars); 
    return new string(chars); 
} 

你會得到後一個角色是否重複1個以上

public static bool IsWordsRepeat(string text) 
{ 
    int count = 0; 
    foreach(char c in text) 
    { 
     count = 0; 
     foreach (char c1 in text) 
     { 
       if (c == c1) 
       { 
        count++; 
       } 
       if (count == 2) 
       return false; 
     } 
    } 
    return true; 
} 

獲取詞最後以排序的方式顯示你的結果。

結果

根據你的問題你問下令像

ab ac ad bc bd cd abc abd acd bcd abcd 

字符串這是不按字母/字典順序,並按如下

enter image description here形成的結果

而現在你需要訂購另一個訂購。只需添加到Console.ReadLine之前,下面的代碼()生成你的期望的結果

var list = dict.Keys.ToList(); 
list.Sort(); 
foreach (var key in list) 
{ 
    Console.WriteLine("{0}", key); 
} 

,顯示如下的結果

enter image description here

+0

代碼轉儲!=答案。至少添加一些評論... – leppie 2014-12-03 06:41:10

+0

我已添加評論。這個答案足夠了嗎? @leppie – 2014-12-03 06:54:07

+0

你試過上面的代碼嗎?得到了解決方案? @Prasad Nair – 2014-12-06 12:09:10

-1
import java.util.*; 
import java.math.*; 

public class combPowerSet { 

    //printing the charachters as per the number sent. 
    void printNumber(int number, char [] items) 
     { 
      String digitString = Integer.toString(number); 
      char [] digitStringArray = digitString.toCharArray(); 
      int length = digitStringArray.length; 
      for (int i=0; i<length; i++) 
      { 
       System.out.print(items[Character.getNumericValue(digitStringArray[i])-1]); 
      } 
      System.out.println(); 
     } 

    //checking if the number follows the required pattern. 
    boolean checkCondition(int number, int itemSize) 
    { 
     boolean validNumber = true; 
     String digitString = Integer.toString(number); 
     char [] digitStringArray = digitString.toCharArray(); 
     int length = digitStringArray.length; 
     for (int i=0; i<length; i++) 
     { 
      for(int j = i+1; j < length; j++) 
      { 
       int x = Character.getNumericValue(digitStringArray[i]); 
       int y = Character.getNumericValue(digitStringArray[j]);    

       if (x > itemSize-1 || y > itemSize || x > y || x==y) 
       { 
        validNumber = false; 
        break; 
       }    
      } 
      if (validNumber == false) break; 
     } 
     return validNumber;  
    } 


    void printCombinations(char [] items) 
    { 
     double maxDigit = 0; 
     int itemSize = items.length; 
     for(int i=1; i<=itemSize; i++) 
     { 
      maxDigit = maxDigit + i*Math.pow(10,itemSize-i); 
     } 


     for(int x=12; x<=maxDigit; x++) 
     { 
      if(checkCondition(x, itemSize)) 
      { 
       printNumber(x, items); 
      } 
     } 
    } 

    public static void main(String [] args) 
    { 
     char [] arr = { 'a','b', 'c','d', 'e'}; 
     combPowerSet obj = new combPowerSet(); 
     obj.printCombinations(arr);  

    } 
} 
+0

顯示你的輸出@Prasad Nair – 2014-12-08 05:12:56