Guest User

Untitled

a guest
May 3rd, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. /*
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2013 Krishna Bharadwaj <krishna@krishnabharadwaj.info>
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. */
  24.  
  25. #include <iostream>
  26. #include <fstream>
  27. #include <map>
  28. #include <vector>
  29. #include <string>
  30. using namespace std;
  31.  
  32. vector <string> words;
  33.  
  34. bool match(string path, string word){
  35. int pl = path.length(), wl = word.length(), j = 0;
  36. for(int i = 0; i < pl; i++){
  37. if(path[i] == word[j])
  38. j++;
  39. }
  40. if(j < wl)
  41. return false;
  42. return true;
  43. }
  44.  
  45. int get_keyboard_row(char c){
  46. const char* args[] = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
  47. vector <string> keyboard(args, args + 3);
  48.  
  49. for(int i = 0; i < keyboard.size(); i++){
  50. int n = keyboard[i].length();
  51. for(int j = 0; j < n; j++){
  52. if(keyboard[i][j] == c)
  53. return i;
  54. }
  55. }
  56. }
  57.  
  58. vector <int> compress(vector <int> s){
  59. int i = 0, j = 0, n = s.size();
  60. while(i < n){
  61. if(s[i] != s[j])
  62. s[++j] = s[i];
  63. i++;
  64. }
  65. return vector <int>(s.begin(), s.begin() + j + 1);
  66. }
  67.  
  68. int get_minimum_wordlength(string path){
  69. int n = path.length();
  70. vector <int> row_numbers(n);
  71. for(int i = 0; i < n; i++){
  72. row_numbers[i] = get_keyboard_row(path[i]);
  73. }
  74.  
  75. int min_length = (compress(row_numbers)).size() - 3;
  76. if(min_length < 0)
  77. min_length = 0;
  78. return min_length;
  79. }
  80.  
  81. vector<string> get_suggestions(string path){
  82. vector <string> suggestions_v1, suggestions_v2, suggestions_v3;
  83. int n = words.size(), pathlen = path.length();
  84. for(int i = 0; i < n; i++){
  85. int len = words[i].length();
  86. if(path[0] == words[i][0] && path[pathlen-1] == words[i][len-1]){
  87. suggestions_v1.push_back(words[i]);
  88. }
  89. }
  90.  
  91. n = suggestions_v1.size();
  92. for(int i = 0; i < n ; i++){
  93. if(match(path, suggestions_v1[i])){
  94. suggestions_v2.push_back(suggestions_v1[i]);
  95. }
  96. }
  97.  
  98. int min_length = get_minimum_wordlength(path);
  99.  
  100. n = suggestions_v2.size();
  101. for(int i = 0; i < n; i++)
  102. if(suggestions_v2[i].length() > min_length)
  103. suggestions_v3.push_back(suggestions_v2[i]);
  104.  
  105. return suggestions_v3;
  106. }
  107.  
  108. void readfile(){
  109. words.clear();
  110. ifstream wordlist_file("wordlist.txt");
  111. string word;
  112. while(wordlist_file >> word)
  113. words.push_back(word);
  114. wordlist_file.close();
  115. }
  116.  
  117. int main(){
  118.  
  119. const char* args[] = {
  120. "heqerqllo", // hello
  121. "qwertyuihgfcvbnjk", // quick
  122. "wertyuioiuytrtghjklkjhgfd", // world
  123. "dfghjioijhgvcftyuioiuytr", // doctor
  124. "aserfcvghjiuytedcftyuytre", // architecture
  125. "asdfgrtyuijhvcvghuiklkjuytyuytre", // agriculture
  126. "mjuytfdsdftyuiuhgvc", // music
  127. "vghjioiuhgvcxsasdvbhuiklkjhgfdsaserty", // vocabulary
  128. };
  129. vector<std::string> paths(args, args + 8);
  130. readfile();
  131.  
  132. for(int i = 0; i < paths.size(); i++){
  133. vector <string> suggestions = get_suggestions(paths[i]);
  134. cout << "[";
  135. for(int j = 0; j < suggestions.size(); j++){
  136. cout << "'" << suggestions[j] << "'";
  137. if(j != suggestions.size() - 1)
  138. cout << ", ";
  139. }
  140. cout << "]" << endl;
  141. }
  142. return 0;
  143. }
Add Comment
Please, Sign In to add comment