2010-04-10 69 views
0

我想生成從文件輸入的字符串單詞的排列。我知道伯爵,並想知道是否有一個簡單的方法來使用數組列表來完成此操作。具有多個字符串單詞的字符串排列

+0

你的意思是,鑑於由數瓦的字符串ords,你想打印所有組成該字符串的單詞的排列?如果是這樣,這是一個簡單而有趣的遞歸(或者如果你喜歡痛苦的話,可以迭代)算法。 – WhirlWind 2010-04-10 16:33:51

回答

0

由於這沒有homework標籤,我建議使用std::vector字和std::next_permutation。 (如果一個homework標籤,我會建議如何實現像std::next_permutation

+0

有時候,所有這些庫都從編程中獲得樂趣;) – WhirlWind 2010-04-10 16:37:49

+0

它不是作業......我有一堆使用SharpNLP編譯的名詞,現在希望使用每個名詞來產生長度爲5的排列。我意識到它需要遞歸方法,但不知道如何實現它,因爲所有的搜索都不會產生明確的方法。但我會繼續檢查謝謝.. – vbNewbie 2010-04-10 19:06:26

1

在MSDN雜誌大文章:String Permutations

Combination Generator in Linq(這其中有一個LINQ基於答案)

使用在上面的鏈接提供的代碼:

string str = "leniel"; 

var permutations = GetPermutations(str); 

foreach (string s in permutations) 
{ 
    Console.WriteLine(s); 
} 

Console.WriteLine(permutations.Count()); // 720 permutations 

Console.ReadLine(); 

更多的鏈接,以幫助:

Listing all permutations of a string/integer

Permutations with LINQ

Generating (word) combinations (permutations) out of a string

Is there a .NET library that can do string permutations or string expansion?

Are there any better methods to do permutation of string?

Generate list of all possible permutations of a string

+0

感謝您的幫助。我剛剛意識到我錯了,我需要做的是將字符串與順序不變但選擇的組合。也應該是n(n-1)/ 2的結果。 – vbNewbie 2010-04-12 03:54:10