# The Stable Marriage Problem

Problem Description The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of: a set M of n males; a set F of n females; for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least). A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A. Given preferable lists of males and females, you must find the male-optimal stable marriage. Input The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females. Output For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases. Sample Input 2 3 a b c A B C a:BAC b:BAC c:ACB A:acb B:bac C:cab 3 a b c A B C a:ABC b:ABC c:BCA A:bac B:acb C:abc Sample Output a A b B c C a B b A c C 代码：http://paste.ubuntu.com/7457457/
1. /*
2.  * Author: Haipz
3.  * School: HDU
4.  * File Name: 1914.cpp
5.  */
6. #include <cstdio>
7. #include <cmath>
8. #include <ctime>
9. #include <cctype>
10. #include <cfloat>
11. #include <cstdlib>
12. #include <cstring>
13. #include <climits>
14. #include <iostream>
15. #include <vector>
16. #include <string>
17. #include <stack>
18. #include <queue>
19. #include <set>
20. #include <map>
21. #include <algorithm>
22. using namespace std;
23. const int N = 50, L = 1;
24. map<string, int> boy;
25. map<string, int> girl;
26. string tb[N + 5], tg[N + 5];
27. string bmap[N + 5][N + 5], gmap[N + 5][N + 5];
28. int b[N + 5][N + 5], g[N + 5][N + 5];
29. int rank[N + 5];
30. int blover[N + 5], glover[N + 5];
31. int main() {
32.     int cs;
33.     cin >> cs;
34.     while (cs--) {
35.         int n;
36.         cin >> n;
37.         boy.clear(), girl.clear();
38.         for (int i = 1; i <= n; ++i) {
39.             cin >> tb[i];
40.             boy[tb[i]] = i;
41.         }
42.         for (int i = 1; i <= n; ++i) {
43.             cin >> tg[i];
44.             girl[tg[i]] = i;
45.         }
46.         getchar();
47.         for (int i = 1; i <= n; ++i) {
48.             string bs = "";
49.             char bc = getchar();
50.             bs += bc;
51.             int u = boy[bs];
52.             getchar();
53.             for (int j = 1; j <= n; ++j) {
54.                 char gc = getchar();
55.                 bmap[u][j] = "";
56.                 bmap[u][j] += gc;
57.             }
58.             getchar();
59.         }
60.         for (int i = 1; i <= n; ++i) {
61.             string gs = "";
62.             char gc = getchar();
63.             gs += gc;
64.             int u = girl[gs];
65.             getchar();
66.             for (int j = 1; j <= n; ++j) {
67.                 char bc = getchar();
68.                 gmap[u][j] = "";
69.                 gmap[u][j] += bc;
70.             }
71.             getchar();
72.         }
73.         for (int i = 1; i <= n; ++i)
74.             for (int j = 1; j <= n; ++j) b[i][j] = girl[bmap[i][j]];
75.         for (int i = 1; i <= n; ++i)
76.             for (int j = 1; j <= n; ++j) g[i][boy[gmap[i][j]]] = j;
77.         memset(blover, 0, sizeof(blover));
78.         memset(glover, 0, sizeof(glover));
79.         fill(rank, rank + n + 1, 1);
80.         bool flag = true;
81.         while (flag) {
82.             flag = false;
83.             for (int i = 1; i <= n; ++i)
84.                 if (!blover[i]) {
85.                     int j = b[i][rank[i]++];
86.                     if (!glover[j]) blover[i] = j, glover[j] = i;
87.                     else if (g[j][i] < g[j][glover[j]]) blover[glover[j]] = 0, blover[i] = j, glover[j] = i;
88.                     flag = true;
89.                 }
90.         }
91.         for (int i = 1; i <= n; ++i) cout << tb[i] << ' ' << tg[blover[i]] << endl;
92.         if (cs) cout << endl;
93.     }
94.     return 0;
95. }