Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <unordered_map>
  7.  
  8. using namespace std;
  9.  
  10. unordered_map<string, vector<string>> c;
  11.  
  12. void generate(vector<string>& links, size_t pos, vector<string> built)
  13. {
  14.     static int64_t longest = 0;
  15.  
  16.     if (pos < links.size())
  17.     {
  18.         generate(links, pos + 1, built);
  19.         built.push_back(links[pos]);
  20.         generate(links, pos + 1, built);
  21.         return;
  22.     }
  23.  
  24.     if (built.size() <= 2)
  25.         return;
  26.  
  27.     for (size_t i = 0; i < built.size(); i++)
  28.         for (size_t j = 0; j < built.size(); j++)
  29.         {
  30.             if (i == j)
  31.                 continue;
  32.             if (find(c[built[i]].begin(), c[built[i]].end(), built[j]) == c[built[i]].end())
  33.                 return;
  34.         }
  35.  
  36.     if (built.size() > longest)
  37.     {
  38.         longest = built.size();
  39.         sort(built.begin(), built.end());
  40.  
  41.         for (size_t i = 0; i < built.size(); i++)
  42.             cout << built[i] << (i == built.size() - 1 ? "" : ",");
  43.         cout << endl;
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     string line, c1, c2;
  50.     ifstream inputfile("input.txt");   
  51.            
  52.     while (getline(inputfile, line))
  53.     {
  54.         c1 = line.substr(0, 2), c2 = line.substr(3);
  55.         if (find(c[c1].begin(), c[c1].end(), c2) == c[c1].end())
  56.             c[c1].push_back(c2), c[c2].push_back(c1);
  57.     }
  58.    
  59.     for (auto& it : c)
  60.         generate(it.second, 0, vector<string>(1, it.first));
  61.  
  62.     inputfile.close();
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement