Guest User

Untitled

a guest
Mar 7th, 2021
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. const int NUMCHARS = 26;
  9. const int MAXCHOICES = 10;
  10. char typos[NUMCHARS][MAXCHOICES];
  11. int choices[NUMCHARS];
  12.  
  13. string *words;
  14. int numWords = 0;
  15.  
  16.  
  17. //return true or false - whether word can be found in words[] array
  18. bool dictionaryWord(string word) {
  19. //CODE HERE
  20. int minimum=0, maximum=numWords-1, half=(minimum+maximum)/2;
  21.  
  22. while(minimum<=maximum)
  23. {
  24. if (words[half]==word)
  25. {
  26. return true;
  27. }
  28. else if (words[half]<word)
  29. {
  30. minimum=half+1;
  31. }
  32. else
  33. {
  34. maximum=half-1;
  35. }
  36. }
  37. return false;
  38. }
  39.  
  40. int alternateWords = 0;
  41.  
  42. //consider all the alternate characters for current index, recurse for all positions
  43. void countAlternates(int index, string word) {
  44. //CODE HERE!
  45. int charIndex=word[index]-'a';
  46.  
  47. for (int i=1; i<=index; i++)
  48. {
  49. word[index]=typos[charIndex][i];
  50.  
  51. if (index==word.length())
  52. cout << word;
  53. }
  54.  
  55.  
  56. }
  57.  
  58. int main() {
  59. ifstream dictionaryFile("words.txt");
  60. string line;
  61.  
  62. //count all dictionary words
  63. while (dictionaryFile >> line)
  64. numWords++;
  65.  
  66. //read all dictionary words
  67. words = new string [numWords];
  68. dictionaryFile.clear();
  69. dictionaryFile.seekg(0, ios::beg);
  70. for(int i=0; i<numWords; i++)
  71. dictionaryFile >> words[i];
  72.  
  73. //typo possibilities for each key
  74. ifstream choicesFile("typo.txt");
  75. for(int i=0; i<NUMCHARS; i++) {
  76. getline(choicesFile, line);
  77. istringstream choiceStr(line);
  78. int numChoices=0;
  79. while (choiceStr >> typos[i][numChoices])
  80. numChoices++;
  81. choices[i] = numChoices;
  82. }
  83.  
  84. //we need to find all possible alternate words for the input word
  85. string searchWord;
  86. cin >> searchWord;
  87. countAlternates(0, searchWord);
  88. if (!alternateWords)
  89. cout << "None";
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment