Anagramer. Find words in dictionary file C++/Qt.

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>

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 we not found we return empty string
    return "";
}

struct OtherWay{
    OtherWay(QStringList list, QStringList finds, QString alternative)
        : list(list), finds(finds), alternative(alternative){
        //        qDebug()  << "Alternative: " << list << finds << alternative;


    }
    QStringList list;
    QStringList finds;
    QString alternative;

};


int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    QStringList args = a.arguments();

    // start word for finding solution
    QString found;
    QString file;
    if(args.size() > 1)
        found = args.at(1);
    if(args.size() > 2)
        file = args.at(2);
 
    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;

    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);

            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;
}

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *