problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
comp526_test.cpp
浏览该文件的文档.
1//
2// Created by wangzhiheng on 26/09/2025.
3//
4#include "comp526.h"
5#include "gtest/gtest.h"
6
7namespace comp526 {
8 namespace stable_matching {
9
10 // Helper function to convert from standard ranked-list preferences
11 // to the priority-matrix format the solution expects.
12 // Input: prefs[i] is a vector of partner IDs, ranked from most to least preferred.
13 // Output: matrix[i][j] is the priority rank student i gives to company j.
14 vector<vector<int>> convert_prefs_to_matrix(const vector<vector<int>> &prefs) {
15 int n = prefs.size();
16 vector<vector<int>> matrix(n, vector<int>(n));
17 for(int i = 0; i < n; ++i) {
18 for(int rank = 0; rank < n; ++rank) {
19 int partner_id = prefs[i][rank];
20 matrix[i][partner_id] = rank;
21 }
22 }
23 return matrix;
24 }
25
26 TEST(StableMatchingTest, Simple2x2) {
27 // A simple case where both students prefer Company 0.
28 // Students:
29 // S0: C0 > C1
30 // S1: C0 > C1
31 // Companies:
32 // C0: S1 > S0
33 // C1: S0 > S1
34 // Expected Student-Optimal Matching: (S0, C1), (S1, C0)
35 // S0 proposes to C0, S1 proposes to C0. C0 prefers S1 and rejects S0.
36 // S0 then proposes to C1, who is free and accepts.
37 vector<vector<int>> s_prefs = {{0, 1}, {0, 1}};
38 vector<vector<int>> c_prefs = {{1, 0}, {0, 1}};
39
40 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
41 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
42
43 Solution sol;
44 vector<int> expected_matches = {1, 0};// ans[0]=C1, ans[1]=C0
45 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
46 }
47
48 TEST(StableMatchingTest, Wikipedia3x3Example) {
49 // This is the classic example from the Stable Marriage Problem Wikipedia page.
50 // Students (Men): A=0, B=1, C=2
51 // Companies (Women): X=0, Y=1, Z=2
52 // Prefs:
53 // A: Y > X > Z -> {1, 0, 2}
54 // B: Z > Y > X -> {2, 1, 0}
55 // C: X > Z > Y -> {0, 2, 1}
56 // X: B > A > C -> {1, 0, 2}
57 // Y: C > B > A -> {2, 1, 0}
58 // Z: A > C > B -> {0, 2, 1}
59 // Expected Student-Optimal (Men-Optimal) Matching: (A,Y), (B,Z), (C,X)
60 vector<vector<int>> s_prefs = {{1, 0, 2}, {2, 1, 0}, {0, 2, 1}};
61 vector<vector<int>> c_prefs = {{1, 0, 2}, {2, 1, 0}, {0, 2, 1}};
62
63 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
64 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
65
66 Solution sol;
67 vector<int> expected_matches = {1, 2, 0};// ans[0]=C1, ans[1]=C2, ans[2]=C0
68 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
69 }
70
71 TEST(StableMatchingTest, MultiRound3x3) {
72 // A case designed to require multiple rounds of proposals and rejections.
73 // Students:
74 // S0: C0 > C1 > C2
75 // S1: C1 > C0 > C2
76 // S2: C0 > C1 > C2
77 // Companies:
78 // C0: S1 > S2 > S0
79 // C1: S0 > S1 > S2
80 // C2: S0 > S1 > S2
81 // Expected Student-Optimal Matching: (S0, C1), (S1, C0), (S2, C2)
82 // Trace:
83 // 1. S0->C0, S1->C1, S2->C0. C0 keeps S2 (better rank), rejects S0. Matches: (S2,C0), (S1,C1).
84 // 2. S0 (free) -> C1. C1 has S1, prefers S0. C1 keeps S0, rejects S1. Matches: (S2,C0), (S0,C1).
85 // 3. S1 (free) -> C0. C0 has S2, prefers S1. C0 keeps S1, rejects S2. Matches: (S1,C0), (S0,C1).
86 // 4. S2 (free) -> C1. C1 has S0, prefers S0. Rejects S2.
87 // 5. S2 (free) -> C2. C2 is free, accepts S2.
88 // Final: (S0,C1), (S1,C0), (S2,C2)
89 vector<vector<int>> s_prefs = {{0, 1, 2}, {1, 0, 2}, {0, 1, 2}};
90 vector<vector<int>> c_prefs = {{1, 2, 0}, {0, 1, 2}, {0, 1, 2}};
91
92 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
93 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
94
95 Solution sol;
96 vector<int> expected_matches = {1, 0, 2};// ans[0]=C1, ans[1]=C0, ans[2]=C2
97 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
98 }
99
100 TEST(StableMatchingTest, WorstCaseForStudents) {
101 // A case where every student gets their last choice.
102 // Students:
103 // S0: C0 > C1
104 // S1: C0 > C1
105 // Companies:
106 // C0: S1 > S0
107 // C1: S1 > S0
108 // Expected Student-Optimal Matching: (S0, C1), (S1, C0)
109 // S0 proposes to C0, S1 proposes to C0. C0 takes S1, rejects S0.
110 // S0 proposes to C1. C1 takes S0. Final: (S0,C1), (S1,C0).
111 // S0 gets 2nd choice. S1 gets 1st choice.
112 vector<vector<int>> s_prefs = {{0, 1}, {0, 1}};
113 vector<vector<int>> c_prefs = {{1, 0}, {1, 0}};
114
115 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
116 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
117
118 Solution sol;
119 vector<int> expected_matches = {1, 0};
120 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
121 }
122
123 }// namespace stable_matching
124 namespace recount {
125 TEST(recout, case1) {
126 istringstream in("Penny Franklin\n"
127 "Connie Froggatt\n"
128 "Barbara Skinner\n"
129 "Connie Froggatt\n"
130 "Jose Antonio Gomez-Iglesias\n"
131 "Connie Froggatt\n"
132 "Bruce Stanger\n"
133 "Barbara Skinner\n"
134 "Barbara Skinner\n"
135 "***");
136 auto out = ostringstream();
137 recount::main(in, out);
138 const auto ans = out.str();
139 ASSERT_EQ("Runoff!", ans);
140 }
141 }// namespace recount
142
143 namespace set {
144 TEST(set, case1) {
145 istringstream in("3DTG 3DOP 2DSG\n1SOP 1DTG 2OTR\n3DOR 3STG 2DSP\n3SSP 3OTG 1DTP\n");
146 auto out = ostringstream();
147 main(in, out);
148 const auto ans = out.str();
149 ASSERT_EQ("1 8 11\n2 9 12\n3 7 12\n5 7 9\n6 8 12\n7 10 11\n", ans);
150 }
151 }// namespace set
152
153 namespace plantingtrees {
155 istringstream in("4\n2 3 4 3\n");
156 auto out = ostringstream();
157 main(in, out);
158 const auto ans = out.str();
159 ASSERT_EQ("7", ans);
160 }
161
163 istringstream in("6\n39 38 9 35 39 20\n");
164 auto out = ostringstream();
165 main(in, out);
166 const auto ans = out.str();
167 ASSERT_EQ("42", ans);
168 }
169 }// namespace plantingtrees
170
171 namespace snowflakes {
172 TEST(snowflakes, case1) {
173 istringstream in("1\n5\n1\n2\n3\n2\n1\n");
174 auto out = ostringstream();
175 main(in, out);
176 const auto ans = out.str();
177 ASSERT_EQ("3", ans);
178 }
179 }// namespace snowflakes
180
181 namespace amalgram {
182 TEST(amalgram, case1) {
183 istringstream in("hello\nworld");
184 auto out = ostringstream();
185 main(in, out);
186 const auto ans = out.str();
187 ASSERT_EQ("dehllorw", ans);
188 }
189
190 TEST(amalgram, case2) {
191 istringstream in("unclear\ninstructions");
192 auto out = ostringstream();
193 main(in, out);
194 const auto ans = out.str();
195 ASSERT_EQ("aceiilnnorssttu", ans);
196 }
197
198 TEST(amalgram, case3) {
199 istringstream in("boring\nboring");
200 auto out = ostringstream();
201 main(in, out);
202 const auto ans = out.str();
203 ASSERT_EQ("bginor", ans);
204 }
205 }// namespace amalgram
206}// namespace comp526
vector< vector< int > > convert_prefs_to_matrix(const vector< vector< int > > &prefs)
TEST(StableMatchingTest, Simple2x2)
int main(istream &cin, ostream &cout)
vector< vector< int > > ans
int main(istream &cin, ostream &cout)
int main(istream &cin, ostream &cout)
TEST(plantingtrees, case1)
TEST(snowflakes, case1)
int main(istream &cin, ostream &cout)
int main(istream &cin, ostream &cout)
vector< int > match(vector< vector< int > >, vector< vector< int > >)