JellyKuo

DS_Homework_5

Jan 10th, 2021 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct Node {
  10.     Node() {
  11.     }
  12.     Node(char character) {
  13.         this->Character = character;
  14.     }
  15.     char Character = '\0';
  16.     bool WordEnd = false;
  17.     vector<Node> Nodes;
  18. };
  19.  
  20. void findAllChanges(string s, vector<string>& l) {
  21.     for (int i = 0; i <= s.size(); i++) {
  22.         if (i >= 1) {
  23.             l.push_back(s.substr(0, i - 1) + s.substr(i));
  24.             l.push_back(s.substr(0, i - 1) + '?' + s.substr(i));
  25.         }
  26.         l.push_back(s.substr(0, i) + '?' + s.substr(i));
  27.         if (i + 1 < s.size()) {
  28.             l.push_back(s.substr(0, i) + s[i + 1] + s[i] + s.substr(i + 2));
  29.         }
  30.     }
  31. }
  32.  
  33. void traverse(Node& n, const string& s, int index, vector<string>& l) {
  34.     if (s.size() == index) {
  35.         if (n.WordEnd) {
  36.             l.push_back(s);
  37.         }
  38.         return;
  39.     }
  40.     for (int i = 0; i < n.Nodes.size(); i++) {
  41.         if (n.Nodes[i].Character == s[index]) {
  42.             traverse(n.Nodes[i], s, index + 1, l);
  43.         }
  44.         else if (s[index] == '?' && n.Nodes[i].Character >= 'a' && n.Nodes[i].Character <= 'z') {
  45.             traverse(n.Nodes[i], s.substr(0, index) + n.Nodes[i].Character + (s.size() == index + 1 ? "" : s.substr(index + 1)), index + 1, l);
  46.         }
  47.     }
  48. }
  49.  
  50. int main() {
  51.     fstream dictStream = fstream("dictionary.txt", ios::in);
  52.     fstream inputStream = fstream("input.txt", ios::in);
  53.     fstream outputStream = fstream("output.txt", ios::out);
  54.     outputStream << "word,answer" << '\n';
  55.  
  56.     Node tree;
  57.  
  58.     string line;
  59.     while (getline(dictStream, line)) {
  60.         if (line.size() == 0 || line[0] == ';') {
  61.             continue;
  62.         }
  63.         Node* nodeRef = nullptr;
  64.         auto startNodeIter = find_if(tree.Nodes.begin(), tree.Nodes.end(), [&](const Node& n) {return n.Character == line[0]; });
  65.         if (startNodeIter != tree.Nodes.end()) {
  66.             nodeRef = &*startNodeIter;
  67.         }
  68.         else {
  69.             tree.Nodes.push_back(Node(line[0]));
  70.             nodeRef = &*(tree.Nodes.end() - 1);
  71.         }
  72.         for (int i = 1; i < line.size(); i++) {
  73.             auto nextNodeIter = find_if(nodeRef->Nodes.begin(), nodeRef->Nodes.end(), [&](const Node& n) {return n.Character == line[i]; });
  74.             if (nextNodeIter != nodeRef->Nodes.end()) {
  75.                 nodeRef = &*nextNodeIter;
  76.             }
  77.             else {
  78.                 nodeRef->Nodes.push_back(Node(line[i]));
  79.                 nodeRef = &*(nodeRef->Nodes.end() - 1);
  80.             }
  81.         }
  82.         nodeRef->WordEnd = true;
  83.     }
  84.  
  85.  
  86.     while (getline(inputStream, line)) {
  87.         vector<string> foundWords = vector<string>();
  88.         outputStream << line << ",";
  89.         traverse(tree, line, 0, foundWords);
  90.         if (foundWords.size() > 0) {
  91.             outputStream << "OK\n";
  92.         }
  93.         else {
  94.             vector<string> changes = vector<string>();
  95.             findAllChanges(line, changes);
  96.             int prevChgSize = changes.size();
  97.             for (int i = 0; i < prevChgSize; i++) {
  98.                 findAllChanges(changes[i], changes);
  99.             }
  100.             sort(changes.begin(), changes.end());
  101.             for (int i = 0; i < changes.size(); i++) {
  102.                 if (i + 1 < changes.size() && changes[i] == changes[i + 1]) {
  103.                     continue;
  104.                 }
  105.                 traverse(tree, changes[i], 0, foundWords);
  106.             }
  107.             sort(foundWords.begin(), foundWords.end());
  108.             //cout << line << "\t" ;
  109.             //for (int i = 0; i < foundWords.size(); i++) {
  110.             //  cout << foundWords[i] << " ";
  111.             //}
  112.             //cout << endl;
  113.             if (foundWords.size() > 0) {
  114.                 for (int i = 0; i < foundWords.size() - 1; i++) {
  115.                     if (foundWords[i] == foundWords[i + 1]) {
  116.                         continue;
  117.                     }
  118.                     outputStream << foundWords[i] << " ";
  119.                 }
  120.                 outputStream << foundWords[foundWords.size() - 1] << "\n";
  121.             }
  122.             else {
  123.                 outputStream << "NONE\n";
  124.             }
  125.         }
  126.     }
  127.  
  128.     outputStream.close();
  129. }
Add Comment
Please, Sign In to add comment