/
Program.cs
91 lines (75 loc) · 2.28 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace anagrammes
{
class Program
{
static void Main(string[] args)
{
string[] content = File.ReadAllLines(@"words.txt");
Anagramme a = new Anagramme();
TreeNode t = new TreeNode(a);
foreach(string word in content)
{
char[] sorted = word.ToCharArray();
Array.Sort(sorted);
addChildren(word, sorted, t);
}
int i = 0;
Action<Anagramme> print =
(Anagramme anag) =>
{
if (anag.refNb > 1)
{
Console.WriteLine(String.Join(", ", anag.words));
i++;
}
};
t.Traverse(print);
Console.WriteLine("{0} anagrammes trouvés", i);
Console.ReadLine();
}
static void addChildren(string word, char[] sorted, TreeNode parentNode)
{
if(sorted.Length ==0)
{
return;
}
TreeNode node = parentNode[sorted[0]];
if (sorted.Length == 1)
{
if (node == null)
{
Anagramme a = new Anagramme();
a.letter = sorted[0];
a.addWord(word);
parentNode.AddChild(a);
}
else
{
node.Value.addWord(word);
}
}
else
{
if (node == null)
{
Anagramme a = new Anagramme();
a.letter = sorted[0];
node = parentNode.AddChild(a);
}
addChildren(word, SubArray(sorted, 1, sorted.Length - 1), node);
}
}
public static char[] SubArray(char[] data, int index, int length)
{
char[] result = new char[length];
Array.Copy(data, index, result, 0, length);
return result;
}
}
}