trafik

Untitled

Mar 8th, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
  3. //#include <ext/pb_ds/tree_policy.hpp> // Содержит класс tree_order_statistics_node_update
  4.  
  5. #define ll long long
  6. #define ull unsigned long long
  7. #define ld long double
  8. #define len(v) (int)v.size()
  9. #define all(v) v.begin(), v.end()
  10. #define rall(v) v.rbegin(), v.rend()
  11. #define pii pair<int, int>
  12. #define vi vector<int>
  13. #define vii vector<vector<int>>
  14. #define vpii vector<pair<int, int>>
  15. #define dcout cout << setprecision(7)
  16. #define mkp make_pair
  17. #define EL '\n'
  18. //#define int long long
  19. //#define ll ull
  20. const int maxn = 1e5 + 10;
  21. const int C = 20;
  22. const int logn = 20;
  23. const int inf = 1e9;
  24. const ll mod = 1073741824;
  25. //const int M = 1e9;
  26. const ll M = 998244353;
  27. const int m1 = (1 << 16), m2 = (1 << 30);
  28. const ld eps = 1e-9;
  29. using namespace std;
  30. //using namespace __gnu_pbds;
  31.  
  32. //typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  33. //ordered_set s;
  34.  
  35. // random
  36. std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count());
  37.  
  38. template<class T>
  39. istream &operator>>(istream &in, vector<T> &a) {
  40.     for (auto &i : a)
  41.         in >> i;
  42.     return in;
  43. }
  44.  
  45. template<class T>
  46. ostream &operator<<(ostream &out, vector<T> &a) {
  47.     for (auto &i : a)
  48.         out << i << ' ';
  49.     return out;
  50. }
  51.  
  52. ifstream fin("a2.txt");
  53. ofstream fout("aboba.txt");
  54.  
  55. int n;
  56. vector<string> a;
  57. multiset<string> b;
  58. bool used[maxn * 2];
  59. vector<int> uind;
  60.  
  61. bool isans = false;
  62. void rec(string up, int cnt = 0) {
  63.     if (up == "AA" && cnt == n && uind[0] == 3) {
  64.         cout << "";
  65.     }
  66.     if (cnt == n) {
  67.         // check & output
  68.  
  69.         vector<string> ans(n);
  70.         ans[0] = up;
  71.         for (int i = 1; i < n; ++i) {
  72.             for (int k : uind) {
  73.                 ans[i] += a[k][i];
  74.             }
  75.         }
  76.         multiset<string> c = b;
  77.         for (auto& s : ans) {
  78.             if (c.find(s) == c.end()) return;
  79.             c.erase(c.find(s));
  80.         }
  81.         for (int k : uind) {
  82.             if (c.find(a[k]) == c.end()) return;
  83.             c.erase(c.find(a[k]));
  84.         }
  85.         if (c.empty()) {
  86.             for (auto &s: ans)
  87.                 cout << s << EL;
  88.             cout << EL;
  89.             isans = true;
  90.         }
  91.         return;
  92.     }
  93.     for (int i = cnt; i < n; ++i) {
  94.         char c = up[i];
  95.         for (int j = 0; j < n * 2; ++j) {
  96.             if (!used[j] && c == a[j][0]) {
  97.                 used[j] = true;
  98.                 uind.push_back(j);
  99.                 if (isans) return;
  100.                 rec(up, cnt + 1);
  101.                 uind.pop_back();
  102.                 used[j] = false;
  103.             }
  104.         }
  105.     }
  106. }
  107.  
  108. void solve() {
  109.     cin >> n;
  110.     a.resize(n * 2); cin >> a;
  111.     for (auto& s : a)
  112.         b.insert(s);
  113.     for (int i = 0; i < n * 2 && !isans; ++i) {
  114.         used[i] = true;
  115.         rec(a[i]);
  116.         used[i] = false;
  117.     }
  118.     isans = false;
  119.     a.clear();
  120.     b.clear();
  121.     uind.clear();
  122.     for (bool& i : used)
  123.         i = false;
  124. }
  125.  
  126. signed main() {
  127.     ios::sync_with_stdio(false);
  128.     cin.tie(nullptr);
  129.     cout.tie(nullptr);
  130.  
  131.     int T = 1;
  132.     cin >> T;
  133.     while (T--) {
  134.         //fout << 2;
  135.         solve();
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment