2016-05-11 5 views
1

Ich habe ein Wörterbuch wie folgt aus (Beispieldaten so macht es keinen Sinn machen):Iterieren Nicht-Quadrat (Art) Matrix

Dictionary<char, string[]> codes = new Dictionary<char, string[]>(); 

string[] eine Reihe möglicher Ersatz für den Schlüssel des Dictionary ist.

das Wörterbuch mit einigen Beispieldaten Auffüllen ...

codes.Add("A", new string[] {"噅噅", "裧", "頖", "11"}); 
codes.Add("B", new string[] {"垥", "2", "鉠"}); 
codes.Add("C", new string[] {"33", "韎"}); 
codes.Add("D", new string[] {"櫋", "緟", "嘕", "鈖", "灡", "犅"}); 
... 
codes.Add("T", new string[] {"濇", "汫", "岕", "5"}); 
... 

Nun lässt „codieren“ das folgende Wort:

char[] charArray = "act".ToCharArray(); 
    foreach (char c in charArray) { 
     string[] replacements = codes[c].Where(x => !String.IsNullOrEmpty(x)).ToArray();//removing empty elements 

     ... 
    } 

ich nicht meinen Kopf wickeln kann jetzt auf das, was als nächstes zu tun, Ich möchte eine Liste aller möglichen Kombinationen haben, sollte eine Liste wie folgt (für das Wort "Akt") zurückgeben:

噅 噅 韎 5

裧 33 濇

裧 33 汫

裧 33 岕

裧 335

裧 韎 濇

裧 韎 汫

裧 韎 岕

...

Es können nicht alle Kombinationen zeigen wegen Spamfilters ... Stackoverflow

+0

Dies wäre ziemlich leicht mit Rekursion gelöst werden. – dharms

+0

@dharms interessant. Wo soll ich die rekursive Methode nennen? Für jeden "Schlüssel"? – Cornwell

+0

Übergeben Sie in zwei Strings, was Sie wollen, codiert und den aktuellen Zweig der codierten Zeichenfolge bisher. Der erste Anruf würde etwa so aussehen: "Kodieren (" act "," ");" – dharms

Antwort

1

Der Post-Titel irreführend ist. Wenn ich es richtig verstehe, möchten Sie alle Kombinationen mit dem codes als Ersatzzeichen ersetzen.

Ich habe eine ähnliche Frage (String combinations with multi-character replacement) beantwortet, aber aufgrund string[] Teil I nicht direkt dem praktischen Code aus Looking at each combination in jagged array wiederverwenden können, also werde ich stattdessen nur den gleichen Algorithmus für Ihren Fall nutzen:

public static class Algorithms 
{ 
    public static IEnumerable<string> GetCombinations(this string input, Dictionary<char, string[]> replacements) 
    { 
     var map = new string[input.Length][]; 
     for (int i = 0; i < map.Length; i++) 
     { 
      var c = input[i]; 
      string[] r; 
      map[i] = replacements.TryGetValue(c, out r) 
       && (r = r.Where(s => !string.IsNullOrEmpty(s)).ToArray()).Length > 0 ? 
       r : new string[] { c.ToString() }; 
     } 
     int maxLength = map.Sum(output => output.Max(s => s.Length)); 
     var buffer = new char[maxLength]; 
     int length = 0; 
     var indices = new int[input.Length]; 
     for (int pos = 0, index = 0; ;) 
     { 
      for (; pos < input.Length; pos++, index = 0) 
      { 
       indices[pos] = index; 
       foreach (var c in map[pos][index]) 
        buffer[length++] = c; 
      } 
      yield return new string(buffer, 0, length); 
      do 
      { 
       if (pos == 0) yield break; 
       index = indices[--pos]; 
       length -= map[pos][index].Length; 
      } 
      while (++index >= map[pos].Length); 
     } 
    } 
} 

Test:

var codes = new Dictionary<char, string[]>(); 
codes.Add('A', new string[] { "噅噅", "裧", "頖", "11" }); 
codes.Add('B', new string[] { "垥", "2", "鉠" }); 
codes.Add('C', new string[] { "33", "韎" }); 
codes.Add('D', new string[] { "櫋", "緟", "嘕", "鈖", "灡", "犅" }); 
//... 
codes.Add('T', new string[] { "濇", "汫", "岕", "5" }); 
//... 

var test = "ACT".GetCombinations(codes).ToList(); 
1

Der Code unten einen schnellen Stich an einer intuitiven rekursive Ansatz zeigt (in einem Button-Klick-Ereignis in einem Formular implemened - ich schlage vor, Sie es eine Darstellung eher als ein soluti berücksichtigen sollten zum direkten Kopieren geeignet!) ...

private void button3_Click(object sender, EventArgs e) 
    { 
     test.Text = "act"; 
     Encoding encoder = new Encoding(); 
     foreach (string encoding in encoder.EncodedStrings(test.Text)) 
     { 
      output.AppendText(encoding + "\n"); 
     } 
    } 

    public class Encoding 
    { 
     public readonly Dictionary<char, string[]> codes = new Dictionary<char, string[]>(); 

     public Encoding() 
     { 
      codes.Add('A', new string[] {"a ", "A ", "a1 ", "A1 "}); 
      codes.Add('B', new string[] {"b ", "B ", "b1 ", "B1 "}); 
      codes.Add('C', new string[] {"c ", "C ", "c1 ", "C1 "}); 
      codes.Add('D', new string[] {"d ", "D ", "d1 ", "D1 "}); 
      codes.Add('T', new string[] {"t ", "T ", "t1 ", "T1 "}); 
     } 

     public List<string> EncodedStrings(string input) 
     { 
      var results = new List<string>(); 

      if (string.IsNullOrWhiteSpace(input)) 
      { 
       results.Add(string.Empty); 
       return results; 
      } 

      // Find the set of replacements for the first character: 
      char  firstChar = input[0]; 
      string[] replacements = this.codes[firstChar.ToString().ToUpperInvariant()[0]]; 

      foreach (string replacement in replacements) 
      { 
       string thisVersion  = replacement; 
       string remainder   = input.Length > 1 ? input.Substring(1) : string.Empty; 
       var remainderVersions = new List<string>(); 

       // Get the various versions of the remainder of the string: 
       remainderVersions.AddRange(EncodedStrings(remainder)); 

       foreach (string remainderVersion in remainderVersions) 
       { 
        results.Add(string.Format("{0}{1}", thisVersion, remainderVersion)); 
       } 
      } 

      return results; 
     } 
    } 

Ich habe eine einfachere Codierung eingefügt, damit die Ergebnisse leichter zu lesen sind. Dies erzeugt die folgende Liste ...

a c t 
a c T 
a c t1 
a c T1 
a C t 
a C T 
a C t1 
a C T1 
a c1 t 
a c1 T 
a c1 t1 
a c1 T1 
a C1 t 
a C1 T 
a C1 t1 
a C1 T1 
A c t 
A c T 
A c t1 
A c T1 
A C t 
A C T 
A C t1 
A C T1 
A c1 t 
A c1 T 
A c1 t1 
A c1 T1 
A C1 t 
A C1 T 
A C1 t1 
A C1 T1 
a1 c t 
a1 c T 
a1 c t1 
a1 c T1 
a1 C t 
a1 C T 
a1 C t1 
a1 C T1 
a1 c1 t 
a1 c1 T 
a1 c1 t1 
a1 c1 T1 
a1 C1 t 
a1 C1 T 
a1 C1 t1 
a1 C1 T1 
A1 c t 
A1 c T 
A1 c t1 
A1 c T1 
A1 C t 
A1 C T 
A1 C t1 
A1 C T1 
A1 c1 t 
A1 c1 T 
A1 c1 t1 
A1 c1 T1 
A1 C1 t 
A1 C1 T 
A1 C1 t1 
A1 C1 T1 
Verwandte Themen