Below sample of code to find word containing the same letters + one that is not the same but create new word:
tor -> sort -> sport
The code can get dictionary and word that should be found:
#include <iostream>
#include <qapplication>
#include <qstringlist>
#include <qfile>
#include <qdebug></qdebug></qfile></qstringlist></qapplication></iostream>
using namespace std;
// oure game of words that matches pattern
QString findWord(QStringList &list, QString myWord)
{
bool pass;
foreach (QString word, list)
{
pass = true;
int i = 0;
while(i < myWord.size())
{
if(!word.contains(myWord.at(i++)))
{
// qDebug() << word <<word.at(i-1); pass="false;" break;="" }="" word="" should="" has="" only="" one="" letter="" that="" can="" be="" other="" than="" we="" think="" if(pass="" &&="" myword.size()="" +1="=" word.size())="" return="" word;="" if="" not="" found="" empty="" string="" "";="" struct="" otherway{="" otherway(qstringlist="" list,="" qstringlist="" finds,="" qstring="" alternative)="" :="" list(list),="" finds(finds),="" alternative(alternative){="" qdebug()="" <<="" "alternative:="" "="" list="" finds="" alternative;="" list;="" finds;="" };="" int="" main(int="" argc,="" char="" *argv[])="" {="" qapplication="" a(argc,="" argv);="" args="a.arguments();" start="" for="" finding="" solution="" found;="" file;="" if(args.size()=""> 1)
found = args.at(1);
if(args.size() > 2)
file = args.at(2);</word.at(i-1);>
QStringList list = {"ail", "tennis", "fails", "desk", "stay","plain", "table", "engine", "sail"};
// read the file with the words
if(!file.isEmpty())
{
QFile inputFile(file);
if(inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// clear default list
list.clear();
QTextStream in(&inputFile);
while (!in.atEnd())
list.append(in.readLine());
inputFile.close();
}
}
// list of finds that match
QStringList finds;
QString tmp;
// append word that we started
finds.append(found);
QList <qstringlist> alternativeList;</qstringlist>
QList <otherway> otherWays;
do
{
do
{
// search for the word
tmp = found;
found = findWord(list, found);
// remove that we found. That protect as from infinite loops
list.removeOne(found);</otherway>
if(!findWord(list,tmp).isEmpty())
{
OtherWay way(list,finds, findWord(list,tmp));
way.finds.append(findWord(list,tmp));
otherWays.append(way);
}
if(!found.isEmpty())
finds.append(found);
}while(!found.isEmpty());
if(otherWays.isEmpty())
break;
alternativeList.append(finds);
list = otherWays.first().list;
found = otherWays.first().alternative;
finds = otherWays.first().finds;
otherWays.removeFirst();
}while(otherWays.size() >= 0);
if(finds.isEmpty())
{
qDebug() << "No solutions!";
return 0;
}
alternativeList.append(finds);
// And finly we find from all alternatives list the longest that we want to print
int id = 0;
int max = 0;
for(int i =0; i < alternativeList.size(); i++)
if(max < alternativeList.at(i).size())
{
id = i;
max = alternativeList.at(i).size();
}
qDebug() << "The longest list: " << alternativeList.at(id);
return 0;
}
#include <qapplication>
#include <qstringlist>
#include <qfile>
#include <qdebug></qdebug></qfile></qstringlist></qapplication></iostream>
using namespace std;
// oure game of words that matches pattern
QString findWord(QStringList &list, QString myWord)
{
bool pass;
foreach (QString word, list)
{
pass = true;
int i = 0;
while(i < myWord.size())
{
if(!word.contains(myWord.at(i++)))
{
// qDebug() << word <<word.at(i-1); pass="false;" break;="" }="" word="" should="" has="" only="" one="" letter="" that="" can="" be="" other="" than="" we="" think="" if(pass="" &&="" myword.size()="" +1="=" word.size())="" return="" word;="" if="" not="" found="" empty="" string="" "";="" struct="" otherway{="" otherway(qstringlist="" list,="" qstringlist="" finds,="" qstring="" alternative)="" :="" list(list),="" finds(finds),="" alternative(alternative){="" qdebug()="" <<="" "alternative:="" "="" list="" finds="" alternative;="" list;="" finds;="" };="" int="" main(int="" argc,="" char="" *argv[])="" {="" qapplication="" a(argc,="" argv);="" args="a.arguments();" start="" for="" finding="" solution="" found;="" file;="" if(args.size()=""> 1)
found = args.at(1);
if(args.size() > 2)
file = args.at(2);</word.at(i-1);>
QStringList list = {"ail", "tennis", "fails", "desk", "stay","plain", "table", "engine", "sail"};
// read the file with the words
if(!file.isEmpty())
{
QFile inputFile(file);
if(inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// clear default list
list.clear();
QTextStream in(&inputFile);
while (!in.atEnd())
list.append(in.readLine());
inputFile.close();
}
}
// list of finds that match
QStringList finds;
QString tmp;
// append word that we started
finds.append(found);
QList <qstringlist> alternativeList;</qstringlist>
QList <otherway> otherWays;
do
{
do
{
// search for the word
tmp = found;
found = findWord(list, found);
// remove that we found. That protect as from infinite loops
list.removeOne(found);</otherway>
if(!findWord(list,tmp).isEmpty())
{
OtherWay way(list,finds, findWord(list,tmp));
way.finds.append(findWord(list,tmp));
otherWays.append(way);
}
if(!found.isEmpty())
finds.append(found);
}while(!found.isEmpty());
if(otherWays.isEmpty())
break;
alternativeList.append(finds);
list = otherWays.first().list;
found = otherWays.first().alternative;
finds = otherWays.first().finds;
otherWays.removeFirst();
}while(otherWays.size() >= 0);
if(finds.isEmpty())
{
qDebug() << "No solutions!";
return 0;
}
alternativeList.append(finds);
// And finly we find from all alternatives list the longest that we want to print
int id = 0;
int max = 0;
for(int i =0; i < alternativeList.size(); i++)
if(max < alternativeList.at(i).size())
{
id = i;
max = alternativeList.at(i).size();
}
qDebug() << "The longest list: " << alternativeList.at(id);
return 0;
}