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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 | #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>
//faire supprimer doublon avec liste apres graphe pour sauvegarde (éventuellement)
//faire serialisation + eventuellement multithreading + optimisation suppression mots déjà 'atteints' + pour recherche anagrammes => mot. + algorithme djikstra
//peut etre bug assign auto tableau
//reoptimise inline et fastcall
//http://www.qtcentre.org/wiki/index.php?title=Profiling_with_GNU_gprof
//http://ftp.traduc.org/doc-vf/gazette-linux/html/2004/100/lg100-L.html
//http://fr.wikipedia.org/wiki/Algorithme_de_tri
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;
bool operator < (InfoMot r) const
{
if (this->Mot.size() < r.Mot.size())
return true;
else if (this->Mot.size() == r.Mot.size())
{
if (*this == r)
return false;
else
{
for (uint i = 0; i < this->Mot.size(); i++)
if (this->Anagramme[i] < r.Anagramme[i])
return true;
else if (r.Anagramme[i] < this->Anagramme[i])
return false;
return false;
}
}
else
return false;
}
bool operator ==(InfoMot r) const
{
return this->Anagramme == r.Anagramme;
}
};
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 set<InfoMot> TraiterMots(QString& mots)
{
set<InfoMot> Mots;
QString tempmotqs;
tempmotqs.reserve(MaxTailleMot);
int itempmot = 0;
QChar lettert;
for (int i = 0; i < TailleMots; 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.insert(tempmot);
tempmotqs.clear();
}
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;
}
};
class Graphe
{
public:
Graphe(set<InfoMot>& motstraites)
{
this->Mots = motstraites;
this->DeterminerIndexs();
}
void GenererGraphe()
{
vector<vector<int>> anagrammes;
anagrammes.reserve(TailleReserveAna);
set<InfoMot>::iterator sit (this->Mots.begin()), send(this->Mots.end());
int i = 0;
for (; sit != send; ++sit)
{
print("Numero du mot : " + QString::number((i + 1)) + " ; mot : " + ArrayToQString((*sit).Mot) + " !");
this->Liens.push_back(vector<int>());
this->FabriquerAnagrammesEtComparer(sit, anagrammes, i);
anagrammes.clear();
i++;
}
}
private:
set<InfoMot> Mots;
vector<vector<int>> Liens;
map<int, vector<int>> Indexs;
void DeterminerIndexs()
{
set<InfoMot>::iterator sit (this->Mots.begin()), send(this->Mots.end());
int i = 0;
uint ilength = 0;
bool deb = true;
for (; sit != send; ++sit)
{
if ((*sit).Anagramme.size() != ilength)
{
if (!deb)
Indexs[ilength].push_back(i);
else
deb = false;
ilength = (*sit).Anagramme.size();
Indexs[ilength] = vector<int>();
Indexs[ilength].push_back(i);
}
i++;
}
}
void Comparer(int size, vector<vector<int>>& anagrammes, int indexp)
{
if (this->Indexs.find(size) != this->Indexs.end())
{
set<InfoMot>::iterator itp = std::next(this->Mots.begin(), this->Indexs[size][0]);
set<InfoMot>::iterator itm = std::next(this->Mots.begin(), this->Indexs[size][1]);
int ip = this->Indexs[size][0];
for (; itp != itm; itp++)
{
if (ip != indexp)
for (uint i = 0; i < anagrammes.size(); i++)
if (anagrammes[i] == (*itp).Anagramme)
this->Liens[indexp].push_back(ip);
ip++;
}
}
}
void FabriquerAnagrammesEtComparer(set<InfoMot>::iterator& sit, vector<vector<int>>& anagrammes, int indexp)
{
if ((*sit).Anagramme.size() < MaxTailleMot)
{
for (int j = 0; j < TailleAlphabet; j++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::PlusLettre, (*sit).Anagramme, j));
this->Comparer((*sit).Anagramme.size() + 1, anagrammes, indexp);
}
anagrammes.clear();
if ((*sit).Anagramme.size() > 1)
{
for (uint j = 0; j < (*sit).Anagramme.size(); j++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::MoinsLetttre, (*sit).Anagramme, j));
this->Comparer((*sit).Anagramme.size() - 1, anagrammes, indexp);
}
anagrammes.clear();
anagrammes.push_back((*sit).Anagramme);
for (uint j = 0; j < (*sit).Anagramme.size(); j++)
for (int k = 0; k < TailleAlphabet; k++)
anagrammes.push_back(this->CreateAnagramme(TypeAnagramme::Modification, (*sit).Anagramme, j, k));
this->Comparer((*sit).Anagramme.size(), anagrammes, indexp);
}
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;
}
};
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 départ + ; + 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");
set<InfoMot> motstraites = Dictionnaire::TraiterMots(mots);
Graphe graphe = Graphe(motstraites);
print("Creation du graphe et des anagrammes !");
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;
}
catch (...)
{
print("ERREUR inconnue !");
return -1;
}
print("\nAu revoir !");
return 0;
}
|