2016-03-22 4 views
-2

私は、文字列の単語を組み合わせてその文字列を組み合わせる最良の方法を見つけようとしています。私はクラスプロジェクトのためにこれをやろうとしています。文字列が "The quick fox"なら、 "Thequick fox"、 "quickfox"、 "thequickfox"を出力する方法を見つける必要があります。私はstring.splitを使用して一緒に戻って接着してみましたが、多くの運がなかった。問題は、文字列の入力は任意のサイズにすることができます。文字列の組み合わせを作るために空白を取り除く方法

+1

あなたの現在の試行を表示できますか?特に失敗している箇所はありますか? – Rob

+1

「n」が単語の数である場合、2 ^(n - 1)の組み合わせを出力する必要があります。 'n <= 30 'の場合、実行可能な時間内に実行されます。私はマスクイテレータをとり、0から2 ^(n - 1)までのすべての値を参照することをお勧めします。組み合わせを構築し、i番目のビットが 'i'番目のスペースが存在するかどうかを調べます。 –

答えて

0

私はこれを楽しむことにしました。ここでのアイデアは、より大きな問題をより小さなサブ問題に分割することです。だから最初は0と1のスペースを持つ文字列から始めました。私は0のスペースで、唯一の可能な組み合わせは文字列アイテムであることが分かります。 1つのスペースで、私はそのスペースを持っていてもいなくてもよい。

次に、基本ケースの1つを得るまで、再帰的に問題を分割する必要があります。それで、私はSkipの分割配列の要素を2ずつ増やします。そのようにして、最終的にベースケースの1つを得ることが保証されます。私がそれをしたら、私はプログラムをもう一度実行し、その結果をすべて私の現在の組み合わせに加える方法を見つけ出す。

class Program 
{ 

    static void Main(string[] args) 
    { 
     string test1 = "fox"; 
     string test2 = "The quick"; 
     string test3 = "The quick fox"; 
     string test4 = "The quick fox says"; 
     string test5 = "The quick fox says hello"; 

     var splittest1 = test1.Split(' '); 
     var splittest2 = test2.Split(' '); 
     var splittest3 = test3.Split(' '); 
     var splittest4 = test4.Split(' '); 
     var splittest5 = test5.Split(' '); 

     var ans1 = getcombinations(splittest1); 
     var ans2 = getcombinations(splittest2); 
     var ans3 = getcombinations(splittest3); 
     var ans4 = getcombinations(splittest4); 
     var ans5 = getcombinations(splittest5); 
    } 




    static List<string> getcombinations(string[] splittest) 
    { 
     var combos = new List<string>(); 
     var numspaces = splittest.Count() - 1; 
     if (numspaces == 1) 
     { 
      var addcombos = AddTwoStrings(splittest[0], splittest[1]); 
      var withSpacesCurrent = addcombos.Item1; 
      var noSpacesCurrent = addcombos.Item2; 
      combos.Add(withSpacesCurrent); 
      combos.Add(noSpacesCurrent); 
     } 
     else if (numspaces == 0) 
     { 
      combos.Add(splittest[0]); 
     } 
     else 
     { 
      var addcombos = AddTwoStrings(splittest[0], splittest[1]); 
      var withSpacesCurrent = addcombos.Item1; 
      var noSpacesCurrent = addcombos.Item2; 
      var futureCombos = getcombinations(splittest.Skip(2).ToArray()); 
      foreach (var futureCombo in futureCombos) 
      { 
       var addFutureCombos = AddTwoStrings(withSpacesCurrent, futureCombo); 
       var addFutureCombosNoSpaces = AddTwoStrings(noSpacesCurrent, futureCombo); 

       var combo1 = addFutureCombos.Item1; 
       var combo2 = addFutureCombos.Item2; 
       var combo3 = addFutureCombosNoSpaces.Item1; 
       var combo4 = addFutureCombosNoSpaces.Item2; 

       combos.Add(combo1); 
       combos.Add(combo2); 
       combos.Add(combo3); 
       combos.Add(combo4); 
      } 
     } 



     return combos; 
    } 

    static Tuple<string, string> AddTwoStrings(string a, string b) 
    { 
     return Tuple.Create(a + " " + b, a + b); 
    } 

} 
} 
0

これは私がそれが最善のアルゴリズムであるかどうかわからない、働いて得た方法である:

は、ここでは、コードです。

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     Console.WriteLine("Enter a string"); 

     string input = Console.ReadLine(); 

     //split the input string into an array 
     string[] arrInput = input.Split(' '); 

     Console.WriteLine("The combinations are..."); 

     //output the original string 
     Console.WriteLine(input); 

     //this loop decide letter combination 
     for (int i = 2; i <= arrInput.Length; i++) 
     { 
      //this loop decide how many outputs we would get for a letter combination 
      //for ex. we would get 2 outputs in a 3 word string if we combine 2 words 
      for (int j = i-1; j < arrInput.Length; j++) 
      { 
       int end = j; // end index 
       int start = (end - i) + 1; //start index 

       string output = Combine(arrInput, start, end); 

       Console.WriteLine(output); 
      } 
     } 

     Console.ReadKey(); 
    } 

    //combine array into a string with space except from start to end 
    public static string Combine(string[] arrInput, int start, int end) { 
     StringBuilder builder = new StringBuilder(); 
     bool combine = false; 

     for (int i = 0; i < arrInput.Length; i++) { 
      //first word in the array... don't worry 
      if (i == 0) { 
       builder.Append(arrInput[i]); 
       continue; 
      } 
      //don't append " " if combine is true 
      combine = (i > start && i <= end) ? true : false; 

      if (!combine) 
      { 
       builder.Append(" "); 
      } 
      builder.Append(arrInput[i]); 
     } 

     return builder.ToString(); 
    } 
} 
+0

あなたのコードでは、入力文字列が「ひよこがこんにちは」と言われている場合、元の文字列の大文字と小文字は区別されません – Ringil

+0

ありがとうございます。私はそれを編集した。 – vabii