Hello i am trying to implement a wordPuzzle
but i am having trouble with the last two functions that i need
to implement.
this is what i am doing.
as i'm reading the dictionary from a text file i am checking the length of the word and according to its length i save it into a vector3 this is just to be able to separate the words by length but at the same time i am inserting i am making connections between the words that have same length and only one of the letters is different.
i have my insertion function already, also the one to make the connections but i was trying to make the search function more efficient because as soon as the user inserts the two words i will use the first one to the list of words with the same length and get the assigned number of that word so that way i can start fromthere to make the connections. once i have the starting point i am suppose to use the breadth first search but i am having a hard time i try doing it but it didn't work.
can you help me!
here is what i have:
this are my classes:
#include<vector>
#include<iostream>
#include<string>
using namespace std;
class info
{
public:
string word;
int number;
vector<unsigned long> vector2;
};
class adjacency_List
{
public:
vector<info>vector3;
int counter;
adjacency_List()
{
counter = 0;
vector3.resize(90000);
}
};
class wordPuzzle
{
public:
adjacency_List arry[20];
void insert(string word1);
unsigned long search(string word);
void makeConnections(int position1, string word1, int position2, string word2);
};
definitions of classes:
#include<iostream>
#include<string>
#include<vector>
#include"driver.h"
void wordPuzzle:: makeConnections(int position1, string word1, int position2, string word2)
{
/* compare the word your inserting with the previous
info. and if the difference of letters is only one
connect them at both locations*/
int difference = 0;
for(int i = 0; i< word1.length(); i++)
{
if(word1[i]!=word2[i])
difference++;
}
if(difference == 1)
{
arry[word1.length()].vector3[position1].vector2.push_back(position2);
arry[word1.length()].vector3[position2].vector2.push_back(position1);
}
}
void wordPuzzle::insert(string word1)
{
/* check the length of the word, and save it into an array
depending on the size. also assign a value to it*/
int l = word1.length();
arry[l].vector3[arry[l].counter].word = word1;
arry[l].vector3[arry[l].counter].number = arry[l].counter;
//arry[l].number = arry[l].counter;
arry[l].counter++; // increment the number
// call the makeconnection function
for(int i = 0; i < arry[l].counter; i++)
{
makeConnections(arry[l].vector3[arry[l].counter-1].number,arry[l].vector3[arry[l].counter-1].word,arry[l].vector3[i].number, arry[l].vector3[i].word);
}
}
unsigned long wordPuzzle:: search(string word)
{
bool found = false;
int i=0;
while(!found)
{
if(arry[word.length()].vector3[i].word == word)
{
return arry[word.length()].vector3[i].number;
cout<<arry[word.length()].vector3[i].number;
}
else
i++;
}
return -1;
}
the dictionary file it has around 80 thousand words
main
#include<fstream>
#include "driver.h"
using namespace std;
int main()
{
string word;
string word2 = "time";
fstream infile;
wordPuzzle list;
infile.open("dictionary.txt");
if(!infile)
cout<<"file does not exist"<<endl;
else
while(!infile.eof())
{
infile>>word;
list.insert(word);
}
//list.makeConnections();
//list.search(word2);
infile.close();
return 0;
}