Advertisement
Sanlover

Untitled

Dec 6th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <algorithm>
  2. #include <Windows.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. using namespace  std;
  8.  
  9. bool is_anagram(string first, string second)
  10. {
  11.     sort(first.begin(), first.end());
  12.     sort(second.begin(), second.end());
  13.  
  14.     return first == second;
  15. }
  16. void func(vector<vector<string>>& correct, vector<string>& result, vector<string>& variant, size_t next_id, size_t& n_words)
  17. {
  18.     for (size_t i = next_id; i < correct.size(); i++)
  19.     {
  20.         string tmp;
  21.         for (size_t j = 0; j < correct[i].size(); j++)
  22.         {
  23.             tmp = correct[i][j];
  24.             vector<string> copy = variant;
  25.             copy.push_back(tmp);
  26.             func(correct, result, copy, i + 1, n_words);
  27.         }
  28.     }
  29.     if (variant.size() == n_words)
  30.     {
  31.         string tmp;
  32.         for (size_t i = 0; i < variant.size(); i++)
  33.             tmp += variant[i] + " ";
  34.         bool is_unique = true;
  35.         for (size_t i = 0; i < result.size(); i++)
  36.             if (result[i] == tmp)
  37.             {
  38.                 is_unique = false;
  39.                 break;
  40.             }
  41.  
  42.         if (is_unique)
  43.             result.push_back(tmp);
  44.     }
  45. }
  46.  
  47. void f(vector<vector<string>>& correct)
  48. {
  49.     vector<string> variant, result;
  50.     size_t size = correct.size();
  51.     vector< size_t > permutation;
  52.  
  53.     for (size_t i = 0; i < size; i++)
  54.         permutation.push_back(i);
  55.  
  56.     do
  57.     {
  58.         for (size_t i = 0; i < permutation.size(); i++)
  59.         {
  60.             swap(correct[i], correct[permutation[i]]);
  61.             func(correct, result, variant, 0, size);
  62.         }
  63.     } while (std::next_permutation(permutation.begin(), permutation.end()));
  64.  
  65.     sort(result.begin(), result.end());
  66.     for (size_t i = 0; i < result.size(); i++)
  67.         cout << result[i] << endl;
  68. }
  69.  
  70. int main()
  71. {
  72.     SetConsoleOutputCP(1251);
  73.     SetConsoleCP(1251);
  74.  
  75.     vector<string>  dictionary;
  76.     string tmp;
  77.  
  78.     ifstream input("input.txt");
  79.     if (!input.is_open())
  80.         return 0;
  81.  
  82.     while (!input.eof())
  83.     {
  84.         getline(input, tmp);
  85.         dictionary.push_back(tmp);
  86.     }
  87.     input.close();
  88.     sort(dictionary.begin(), dictionary.end());
  89.  
  90.     cout << "Enter the word = ";
  91.     getline(cin, tmp);
  92.  
  93.     size_t n_words = 1;
  94.     for (auto& it : tmp) if (it == ' ') n_words++;
  95.  
  96.     vector<vector<string>> correct(n_words);
  97.     for (size_t i = 0; i < n_words; i++)
  98.     {
  99.         string first = tmp.substr(0, tmp.find(' '));
  100.         for (auto& it : dictionary)
  101.             if (is_anagram(first, it))
  102.             {
  103.                 correct[i].push_back(it);
  104.             }
  105.         tmp.erase(0, tmp.find(' ') + 1);
  106.     }
  107.  
  108.     cout << endl << "Result:" << endl;
  109.     f(correct);
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement