Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <ctime>
- #include <vector>
- #include <stack>
- #include <map>
- #include <set>
- #include <queue>
- #include <cstring>
- #include <bitset>
- #include <cstdio>
- #include <cstdlib>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef double db;
- typedef long double ldb;
- typedef string str;
- #define forn(i, n) for(int i = 0; i < n; ++i)
- #define INF 1e+9
- #define EPS 1e-9
- #define PI 3.1415926535897932384626433832795
- #define mp(a, b) make_pair(a, b)
- #define pb(a) push_back(a)
- #define X first
- #define Y second
- #define ms(x) memset(x, 0, sizeof(x))
- #define ms1(x) memset(x, -1, sizeof(x))
- #define sz(x) ( (int) x.size() )
- #define len(x) ( (int) x.length() )
- #define ALL(x) x.begin(), x.end()
- #define pii pair<int, int>
- #define pdd pair<double, double>
- #define pll pair<ll, ll>
- map<string, vector<string> > arr;
- bool can(vector<string> templ) {
- vector<string> pat;
- pat.push_back("A");
- pat.push_back("B");
- pat.push_back("C");
- bool ok = true;
- forn(iter, sz(pat)) {
- int times = 0;
- int index = -1;
- forn(i, sz(templ)) {
- if (templ[i].find(pat[iter]) != -1) {
- if (index == -1) {
- times++;
- index = i;
- }
- }
- else {
- index = -1;
- }
- }
- if (times >= 2)
- ok = false;
- }
- return ok;
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("b.in", "r", stdin);
- freopen("b.out", "w", stdout);
- #endif
- int n;
- cin >> n;
- forn(i, n) {
- string a, b;
- cin >> a >> b;
- arr[a].push_back(b);
- }
- vector<string> templ;
- for (map<string, vector<string> >::iterator it = arr.begin(); it != arr.end(); ++it) {
- templ.push_back((*it).X);
- }
- sort(ALL(templ));
- bool ok = false;
- do {
- ok = can(templ);
- if (ok) {
- forn(iter, sz(templ)) {
- vector<string> ans = arr[templ[iter]];
- forn(i, sz(ans)) {
- cout << templ[iter] << " " << ans[i] << "\n";
- }
- }
- break;
- }
- } while (next_permutation(ALL(templ)));
- if (!ok) {
- cout << "Impossible\n";
- }
- #ifdef _DEBUG
- cout << fixed << setprecision(15) << clock() * 1. / CLOCKS_PER_SEC;
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement