Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Themengebiete » Praktische Informatik » Statistische Analyse mit Substitutionschiffren » Hallo Gast [Anmelden|Registrieren]
Letzter Beitrag | Erster ungelesener Beitrag Druckvorschau | An Freund senden | Thema zu Favoriten hinzufügen
Seiten (2): « vorherige 1 [2] Neues Thema erstellen Antwort erstellen
Zum Ende der Seite springen Statistische Analyse mit Substitutionschiffren
Autor
Beitrag « Vorheriges Thema | Nächstes Thema »
eulerscheZahl eulerscheZahl ist männlich
Foren Gott


Dabei seit: 04.01.2013
Beiträge: 2.859

Auf diesen Beitrag antworten Zitatantwort auf diesen Beitrag erstellen Diesen Beitrag editieren/löschen Diesen Beitrag einem Moderator melden       Zum Anfang der Seite springen

So, habe mich mal an einer Umsetzung versucht (sicher noch an einigen Stellen zu verbessern).
Habe mir von hier eine Wortliste besorgt: http://www.fernuni-hagen.de/mathinf/stud...rtraetsel.shtml

Weil ich keine Lust auf Abtippen hatte, habe ich nur die horizontalen Worte geprüft:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
QUERLAGE  OPTIMIST  RIA       NAVARRA   OST       ALAAF     OMA       AIR       
NARBE     LEH       FELS      MIDI      OIE       BENGALI   BUS       FELCHEN   
LEI       LETAL     RIO       REVAL     LAUTLOS   AGNES     ANIS      ZART      
ARES      NANA      SEM       DAEMMEN   MAE       NATO      LAI       FLACHS    
TYP       YOGI      ASYL      EID       KAMERA    ETA       OSTE      RUR       
SPRINTS   ASA       VIZE      DEO       EST       EBRO      OWENS     EMAILLE   
PLAID     ZOO       PIQUE     MAE       INTERNA   SUK       JERICHO   BOR       
ROCK      JERICHO   BOR       ROCK      BETA      HOF       AWARE     HAG       
PAN       BEATE     VAN       ITALIEN   EDO       CENTIMES  KRYOLITH  
58 bekannte Worte
22=P, 1=R, 6=I, 11=M, 10=A, 5=E, 4=O, 2=L, 15=S, 3=N, 7=T, 19=B, 25=H, 23=C, 9=U, 26=D, 12=G, 17=K, 20=F, 8=V, 21=Y, 18=Z, 13=W, 14=Q, 24=J


Das ist die Datei puzzle.txt (nur dein Scrrenshot abgetippt, zumindest die waagerechten Worte eben:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
14 9 5 1 2 10 12 5
4 22 7 6 11 6 15 7
1 6 10
3 10 8 10 1 1 10
4 15 7
10 2 10 10 20
4 11 10
10 6 1
3 10 1 19 5
2 5 25
20 5 2 15
11 6 26 6
4 6 5
19 5 3 12 10 2 6
19 9 15
20 5 2 23 25 5 3
2 5 6
2 5 7 10 2
1 6 4
1 5 8 10 2
2 10 9 7 2 4 15
10 12 3 5 15
10 3 6 15
18 10 1 7
10 1 5 15
3 10 3 10
15 5 11
26 10 5 11 11 5 3
11 10 5
3 10 7 4
2 10 6
20 2 10 23 25 15
7 21 22
21 4 12 6
10 15 21 2
5 6 26
17 10 11 5 1 10
5 7 10
4 15 7 5
1 9 1
15 22 1 6 3 7 15
10 15 10
8 6 18 5
26 5 4
5 15 7
5 19 1 4
4 13 5 3 15
5 11 10 6 2 2 5
22 2 10 6 26
18 4 4
22 6 14 9 5
11 10 5
6 3 7 5 1 3 10
15 9 17
24 5 1 6 23 25 4
19 4 1
1 4 23 17
24 5 1 6 23 25 4
19 4 1
1 4 23 17
19 5 7 10
25 4 20
10 13 10 1 5
25 10 12
22 10 3
19 5 10 7 5
8 10 3
6 7 10 2 6 5 3
5 26 4
23 5 3 7 6 11 5 15
17 1 21 4 2 6 7 25



Und der Quellcode (C#):
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace infoboard
{
	class MainClass
	{
		static string[] dict;
		static int[][] puzzle;
		static int bestFit;
		static void Main(string[] args) {
			string path = "/home/eulerschezahl/Downloads/";
			//einlesen
			dict = File.ReadLines (path + "worte.txt").Select (x => x.Substring (0, x.IndexOf (" "))).ToArray ();
			puzzle = File.ReadLines (path + "puzzle.txt").Select (x => x.Split(' ').Select(y => int.Parse(y)).ToArray()).ToArray ();
			bestFit = puzzle.Length * 3 / 4;

			//buchstabenhäufigkeit
			List<KeyValuePair<int,int>> letterCount = new List<KeyValuePair<int,int>> ();
			for (int i = 0; i <= 26; i++)
				letterCount.Add (new KeyValuePair<int, int>(i, 0));
			foreach (int[] word in puzzle)
				foreach (int i in word)
					letterCount [i] = new KeyValuePair<int, int> (i, letterCount [i].Value + 1);
			letterCount = letterCount.OrderBy (x => -x.Value).Where(x => x.Value > 0).ToList();
			letterCount = letterCount.Where (x => (new List<int> { 1, 6, 11, 22 }).IndexOf (x.Key) < 0).ToList ();
			int[] nextLetter = letterCount.Select (x => x.Key).ToArray();

			//Zuweisung vordefinierter Buchstaben P, R, I, M
			bool[] used = new bool[26];
			Dictionary<int,char> letters = new Dictionary<int, char> ();
			letters.Add (22, 'P'); used ['P' - 'A'] = true;
			letters.Add (1, 'R');used ['R' - 'A'] = true;
			letters.Add (6, 'I');used ['I' - 'A'] = true;
			letters.Add (11, 'M');used ['M' - 'A'] = true;

			FindLetters (letters, used, nextLetter, 0);
		}

		static void FindLetters(Dictionary<int, char> letters, bool[] used, int[] nextLetter, int index) {
			int fit = CountFit (letters);
			if (fit < bestFit)
				return;
			if (index == nextLetter.Length) { //Alle Buchstaben zugeteilt, Bilanz ziehen und gegebenenfalls Ergebnis ausgeben
				bestFit = fit;
				foreach (int[] word in puzzle) {
					StringBuilder sb = new StringBuilder ();
					foreach (int i in word) {
						sb.Append (letters.ContainsKey (i) ? letters [i] : '?');
					}
					Console.Write (sb.ToString().PadRight(10));
				}
				Console.Write ("\n" + fit + " bekannte Worte\n" + String.Join (", ", letters.Select (x => x.Key + "=" + x.Value)) + "\n\n");
				return;
			}

			List<KeyValuePair<char, int>> fitCount = new List<KeyValuePair<char, int>> ();
			for (char c = 'A'; c <= 'Z'; c++) {
				if (used [c - 'A'])
					continue;
				Dictionary<int, char> newDict = new Dictionary<int, char> (letters);
				newDict.Add (nextLetter [index], c);
				fitCount.Add (new KeyValuePair<char, int>(c, CountFit (newDict)));
			}
			fitCount = fitCount.OrderBy (x => -x.Value).ToList();
			foreach(KeyValuePair<char, int> pair in fitCount) {
				if (pair.Value < bestFit)
					return;
				Dictionary<int, char> newDict = new Dictionary<int, char> (letters);
				newDict.Add (nextLetter [index], pair.Key);
				bool[] newUsed = (bool[])used.Clone ();
				newUsed [pair.Key - 'A'] = true;
				FindLetters (newDict, newUsed, nextLetter, index + 1);
			}
		}

		private static int CountFit(Dictionary<int, char> letters) {
			int result = 0;
			foreach (int[] word in puzzle) {
				StringBuilder regex = new StringBuilder ();
				foreach (int i in word) {
					regex.Append (letters.ContainsKey (i) ? letters [i] : '.');
				}
				bool match = false;
				string pattern = regex.ToString ();
				foreach (string s in dict) {
					if (s.Length == word.Length && Regex.IsMatch (s, pattern)) {
						match = true; break;
					}
				}
				if (match)
					result++;
			}
			return result;
		}
	}
}


__________________
Syntax Highlighting fürs Board (Link)

Dieser Beitrag wurde 1 mal editiert, zum letzten Mal von eulerscheZahl: 19.06.2015 16:58.

19.06.2015 16:56 eulerscheZahl ist offline Beiträge von eulerscheZahl suchen Nehmen Sie eulerscheZahl in Ihre Freundesliste auf
Seiten (2): « vorherige 1 [2] Baumstruktur | Brettstruktur
Gehe zu:
Neues Thema erstellen Antwort erstellen
Informatiker Board » Themengebiete » Praktische Informatik » Statistische Analyse mit Substitutionschiffren