Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
- //#include <ext/pb_ds/tree_policy.hpp> // Содержит класс tree_order_statistics_node_update
- #define ll long long
- #define ull unsigned long long
- #define ld long double
- #define len(v) (int)v.size()
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define pii pair<int, int>
- #define vi vector<int>
- #define vii vector<vector<int>>
- #define vpii vector<pair<int, int>>
- #define dcout cout << setprecision(7)
- #define mkp make_pair
- #define EL '\n'
- //#define int long long
- //#define ll ull
- const int maxn = 1e5 + 10;
- const int C = 20;
- const int logn = 20;
- const int inf = 1e9;
- const ll mod = 1073741824;
- //const int M = 1e9;
- const ll M = 998244353;
- const int m1 = (1 << 16), m2 = (1 << 30);
- const ld eps = 1e-9;
- using namespace std;
- //using namespace __gnu_pbds;
- //typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
- //ordered_set s;
- // random
- std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count());
- template<class T>
- istream &operator>>(istream &in, vector<T> &a) {
- for (auto &i : a)
- in >> i;
- return in;
- }
- template<class T>
- ostream &operator<<(ostream &out, vector<T> &a) {
- for (auto &i : a)
- out << i << ' ';
- return out;
- }
- ifstream fin("a2.txt");
- ofstream fout("aboba.txt");
- int n;
- vector<string> a;
- multiset<string> b;
- bool used[maxn * 2];
- vector<int> uind;
- bool isans = false;
- void rec(string up, int cnt = 0) {
- if (up == "AA" && cnt == n && uind[0] == 3) {
- cout << "";
- }
- if (cnt == n) {
- // check & output
- vector<string> ans(n);
- ans[0] = up;
- for (int i = 1; i < n; ++i) {
- for (int k : uind) {
- ans[i] += a[k][i];
- }
- }
- multiset<string> c = b;
- for (auto& s : ans) {
- if (c.find(s) == c.end()) return;
- c.erase(c.find(s));
- }
- for (int k : uind) {
- if (c.find(a[k]) == c.end()) return;
- c.erase(c.find(a[k]));
- }
- if (c.empty()) {
- for (auto &s: ans)
- cout << s << EL;
- cout << EL;
- isans = true;
- }
- return;
- }
- for (int i = cnt; i < n; ++i) {
- char c = up[i];
- for (int j = 0; j < n * 2; ++j) {
- if (!used[j] && c == a[j][0]) {
- used[j] = true;
- uind.push_back(j);
- if (isans) return;
- rec(up, cnt + 1);
- uind.pop_back();
- used[j] = false;
- }
- }
- }
- }
- void solve() {
- cin >> n;
- a.resize(n * 2); cin >> a;
- for (auto& s : a)
- b.insert(s);
- for (int i = 0; i < n * 2 && !isans; ++i) {
- used[i] = true;
- rec(a[i]);
- used[i] = false;
- }
- isans = false;
- a.clear();
- b.clear();
- uind.clear();
- for (bool& i : used)
- i = false;
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T = 1;
- cin >> T;
- while (T--) {
- //fout << 2;
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment