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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 | #include <QCoreApplication>
#include <QString>
#include <QStringBuilder>
#include <QFile>
#include <QResource>
#include <QRegExp>
#include <QIODevice>
#include <QList>
#include <QVariant>
#include <QDataStream>
#include <QSettings>
#include <QChar>
#include <iostream>
#include <string>
#include <chrono>
#include <vector>
#include <thread>
#include <algorithm>
#include <array>
#include <set>
using namespace std;
typedef unsigned int uint;
enum TypeAnagramme { PlusLettre, MoinsLetttre, Modification };
namespace
{
const int Alphabet[26] = { 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 };
const int TailleMots = 336531;
const int MaxTailleMot = 25;
const int TailleAlphabet = 26;
const int TailleReserveAna = 351;
const QChar Ar = 'a';
const QString Aa = "âàä";
const QChar Er = 'e';
const QString Ea = "éèêë";
const QChar Ir = 'i';
const QString Ia = "îï";
const QChar Or = 'o';
const QString Oa = "ôö";
const QChar Ur = 'u';
const QString Ua = "ùûü";
const QChar Yr = 'y';
const QString Ya = "ÿ";
const QChar RetourLigne = '\n';
const string OptionSansTexte = "nocout";
bool AfficherLesInfos = true;
QString MotDeDepart = "";
QString MotAAtteindre = "";
}
void print(QString text, bool newline = true)
{
if (AfficherLesInfos)
{
cout << text.toLatin1().constData();
if (newline)
cout << endl;
}
}
QString ArrayToQString(vector<int>& str)
{
QString ret = "";
ret.reserve(str.size());
for (uint i = 0; i < str.size(); i++)
ret.append((char)str[i]);
return ret;
}
class InfoMot
{
public:
vector<int> Mot;
vector<int> Anagramme;
vector<InfoMot*> PossibilitesIndex;
bool operator==(InfoMot d) const
{
if (this->Anagramme.size() == d.Anagramme.size())
return (this->Anagramme == d.Anagramme);
else
return false;
}
bool operator!=(InfoMot d) const
{
return (!(*this == d));
}
bool operator>(InfoMot d) const
{
if ((*this != d) && (d < *this))
return true;
else
return false;
}
bool operator<(InfoMot d) const
{
if (this->Anagramme.size() == d.Anagramme.size())
{
for (uint i = 0; i < this->Anagramme.size(); i++)
if (this->Anagramme[i] < d.Anagramme[i])
return true;
else if (this->Anagramme[i] > d.Anagramme[i])
return false;
return false;
}
else if (this->Anagramme.size() < d.Anagramme.size())
return true;
else
return false;
}
};
class Dictionnaire
{
public:
static QString LireDictionnaire(QString dicochemin)
{
QFile filedico(dicochemin);
filedico.open(QIODevice::ReadOnly);
QByteArray bytes = filedico.readAll();
filedico.close();
return ((QString)bytes.constData());
}
static vector<InfoMot> TraiterMots(QString& mots)
{
vector<InfoMot> Mots;
Mots.reserve(TailleMots);
QString tempmotqs;
tempmotqs.reserve(MaxTailleMot);
int itempmot = 0;
QChar lettert;
for (int i = 0; i < mots.length(); i++)
{
do
{
if (itempmot < mots.length())
{
lettert = mots.at(itempmot);
itempmot++;
DeleteAccents(Aa, Ar, lettert);
DeleteAccents(Ea, Er, lettert);
DeleteAccents(Ia, Ir, lettert);
DeleteAccents(Oa, Or, lettert);
DeleteAccents(Ua, Ur, lettert);
DeleteAccents(Ya, Yr, lettert);
tempmotqs.append(lettert);
}
else
break;
}
while ((itempmot < mots.length()) && (mots.at(itempmot) != RetourLigne));
itempmot++;
InfoMot tempmot = InfoMot();
tempmot.Mot = QStringToAInt(tempmotqs);
tempmot.Anagramme = tempmot.Mot;
sort(tempmot.Anagramme.begin(), tempmot.Anagramme.end());
Mots.push_back(tempmot);
tempmotqs.clear();
}
sort(Mots.begin(), Mots.end());
Dictionnaire::SupprimerDoublons(Mots);
return Mots;
}
private:
static void DeleteAccents(const QString& acc, const QChar& remp, QChar& letter)
{
for (int i = 0; i < acc.size(); i++)
if (acc.at(i) == letter)
letter = remp;
}
static vector<int> QStringToAInt(QString& mot)
{
vector<int> valinint;
valinint.reserve(mot.length());
for (int i = 0; i < mot.length(); i++)
valinint.push_back((int)(mot.at(i).toLatin1()));
return valinint;
}
static void SupprimerDoublons(vector<InfoMot>& Mots)
{
std::vector<InfoMot>::iterator r, w;
std::set<InfoMot> tmpset;
for (r = Mots.begin(), w = Mots.begin(); r != Mots.end(); ++r)
if (tmpset.insert(*r).second)
*w++ = *r;
Mots.erase(w, Mots.end());
}
};
class Graphe
{
public:
Graphe(vector<InfoMot>& motstraites)
{
this->Mots = motstraites;
}
void GenererGraphe()
{
vector<vector<int>> anagrammes;
anagrammes.reserve(TailleReserveAna);
for (uint i = 0; i < this->Mots.size(); i++)
{
//print("Numero du mot : " + QString::number((i + 1)) + " ; mot : " + ArrayToQString(this->Mots[i].Mot) + " !");
anagrammes.push_back(this->Mots[i].Anagramme);
this->FabriquerAnagrammes(i, anagrammes);
this->ComparerAnagrammes(i, anagrammes);
anagrammes.clear();
}
}
private:
vector<InfoMot> Mots;
void ComparerAnagrammes(uint i, vector<vector<int>>& anagrammes)
{
vector<InfoMot>::iterator it;
InfoMot temp;
uint index = 0;
for (uint k = 0; k < anagrammes.size(); k++)
{
temp.Anagramme = anagrammes[k];
it = std::lower_bound(this->Mots.begin(), this->Mots.end(), temp);
if (it != this->Mots.end())
{
index = it - this->Mots.begin();
if (i != index)
this->Mots[i].PossibilitesIndex.push_back(&this->Mots[index]);
}
}
}
void FabriquerAnagrammes(int i, vector<vector<int>>& anagrammes)
{
if (this->Mots[i].Anagramme.size() < MaxTailleMot)
for (int j = 0; j < TailleAlphabet; j++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::PlusLettre, this->Mots[i].Anagramme, j));
if (this->Mots[i].Anagramme.size() > 1)
for (uint j = 0; j < this->Mots[i].Anagramme.size(); j++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::MoinsLetttre, this->Mots[i].Anagramme, j));
for (uint j = 0; j < this->Mots[i].Anagramme.size(); j++)
for (int k = 0; k < TailleAlphabet; k++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::Modification, this->Mots[i].Anagramme, j, k));
}
vector<int> CreateAnagramme(TypeAnagramme type, vector<int>& mot, uint j, uint k = 0)
{
vector<int> motmodifie = mot;
switch (type)
{
case TypeAnagramme::PlusLettre:
motmodifie.push_back(Alphabet[j]);
break;
case TypeAnagramme::MoinsLetttre:
motmodifie.erase(motmodifie.begin() + j);
break;
case TypeAnagramme::Modification:
motmodifie[j] = Alphabet[k];
break;
}
sort(motmodifie.begin(), motmodifie.end());
return motmodifie;
}
};
static Graphe* _graphe = nullptr;
namespace General
{
void ChargerMot(int argc, char *argv[])
{
if (argc >= 3)
{
MotDeDepart = argv[1];
MotAAtteindre = argv[2];
if (argc == 4)
if (((string)argv[3]) == OptionSansTexte)
AfficherLesInfos = false;
}
else
{
print("Entrer le mot de depart + ; + le mot d'arrivé : ", false);
string ep = "";
cin >> ep;
int eps = ep.find(';');
MotDeDepart = ep.substr(0, eps).c_str();
MotAAtteindre = ep.substr((eps + 1), ep.size()).c_str();
}
}
void VerifierEtCreerGraphe()
{
if (!QFile("dic.graph").exists())
{
print("Chargement du dictionnaire et suppression des accents !");
QString mots = Dictionnaire::LireDictionnaire(":/motsfr/words.txt");
vector<InfoMot> motstraites = Dictionnaire::TraiterMots(mots);
_graphe = new Graphe(motstraites);
print("Creation du graphe et des anagrammes (cette operation peut prendre quelques minutes) !");
_graphe->GenererGraphe();
//TODO (faire serialisation)
}
}
}
int main(int argc, char *argv[])
{
print("Bonjour !\n");
try
{
General::ChargerMot(argc, argv);
print("Mot de depart : " + MotDeDepart + " ; Mot d'arrive : " + MotAAtteindre + " !\n");
auto start = std::chrono::high_resolution_clock::now();
General::VerifierEtCreerGraphe();
print("Chargement du graphe ! TODO");
//TODO (chargement du graphe sérialisé)
print("Calcul... TODO");
//TODO (dijkstra's algorithm)
std::cout << "Temps : " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()-start).count() << " ms !" << std::endl;
delete _graphe;
}
catch (...)
{
print("ERREUR inconnue !");
return -1;
}
print("\nAu revoir !");
return 0;
}
|