Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <string>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <vector>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <queue>
  16. #include <cstring>
  17. #include <bitset>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <cassert>
  21.  
  22. using namespace std;
  23.  
  24. typedef long long ll;
  25. typedef double db;
  26. typedef long double ldb;
  27. typedef string str;
  28.  
  29. #define forn(i, n) for(int i = 0; i < n; ++i)
  30. #define INF 1e+9
  31. #define EPS 1e-9
  32. #define PI 3.1415926535897932384626433832795
  33. #define mp(a, b) make_pair(a, b)
  34. #define pb(a) push_back(a)
  35. #define X first
  36. #define Y second
  37. #define ms(x) memset(x, 0, sizeof(x))
  38. #define ms1(x) memset(x, -1, sizeof(x))
  39. #define sz(x) ( (int) x.size() )
  40. #define len(x) ( (int) x.length() )
  41. #define ALL(x) x.begin(), x.end()
  42. #define pii pair<int, int>
  43. #define pdd pair<double, double>
  44. #define pll pair<ll, ll>
  45.  
  46. map<string, vector<string> > arr;
  47.  
  48. bool can(vector<string> templ) {
  49.     vector<string> pat;
  50.     pat.push_back("A");
  51.     pat.push_back("B");
  52.     pat.push_back("C");
  53.     bool ok = true;
  54.     forn(iter, sz(pat)) {
  55.         int times = 0;
  56.         int index = -1;
  57.         forn(i, sz(templ)) {
  58.             if (templ[i].find(pat[iter]) != -1) {
  59.                 if (index == -1) {
  60.                     times++;
  61.                     index = i;
  62.                 }
  63.             }
  64.             else {
  65.                 index = -1;
  66.             }  
  67.         }
  68.         if (times >= 2)
  69.             ok = false;
  70.     }
  71.     return ok;
  72. }
  73.  
  74. int main() {
  75. #ifdef _DEBUG
  76.     freopen("input.txt", "r", stdin);
  77.     freopen("output.txt", "w", stdout);
  78. #else
  79.     freopen("b.in", "r", stdin);
  80.     freopen("b.out", "w", stdout);
  81. #endif
  82.     int n;
  83.     cin >> n;
  84.     forn(i, n) {
  85.         string a, b;
  86.         cin >> a >> b;
  87.         arr[a].push_back(b);
  88.     }
  89.     vector<string> templ;
  90.     for (map<string, vector<string> >::iterator it = arr.begin(); it != arr.end(); ++it) {
  91.         templ.push_back((*it).X);
  92.     }
  93.     sort(ALL(templ));
  94.     bool ok = false;
  95.     do {
  96.         ok = can(templ);
  97.         if (ok) {
  98.             forn(iter, sz(templ)) {
  99.                 vector<string> ans = arr[templ[iter]];
  100.                 forn(i, sz(ans)) {
  101.                     cout << templ[iter] << " " << ans[i] << "\n";
  102.                 }
  103.             }
  104.             break;
  105.         }
  106.     } while (next_permutation(ALL(templ)));
  107.     if (!ok) {
  108.         cout << "Impossible\n";
  109.     }
  110. #ifdef _DEBUG
  111.     cout << fixed << setprecision(15) << clock() * 1. / CLOCKS_PER_SEC;
  112. #endif
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement