problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
comp526.cpp
浏览该文件的文档.
1//
2// Created by wangzhiheng on 26/09/2025.
3//
4#include "comp526.h"
5
6#include <algorithm>
7#include <climits>
8#include <iostream>
9#include <ostream>
10#include <sstream>
11#include <vector>
12
13using namespace std;
14
15namespace comp526 {
16 namespace stable_matching {
17 vector<int> Solution::match(vector<vector<int>> s2c, vector<vector<int>> c2s) {
18 int N = s2c.size();
19 students = vector<Student *>();
20 companies = vector<Company *>();
21 for(int i = 0; i < N; i++) {
22 Student *stu = new Student(i);
23 stu->c_priority = unordered_map<Company *, int>();
24 stu->priority_c = unordered_map<int, Company *>();
25 students.emplace_back(stu);
26 Company *com = new Company(i);
27 com->priority = unordered_map<Student *, int>();
28 companies.emplace_back(com);
29 }
30 for(int i = 0; i < N; i++) {
31 for(int j = 0; j < N; j++) {
32 students[i]->c_priority[companies[j]] = s2c[i][j];
33 students[i]->priority_c[s2c[i][j]] = companies[j];
34 companies[i]->priority[students[j]] = c2s[i][j];
35 }
36 }
37 bool flag = true;
38 while(flag) {
39 flag = false;
40 for(int i = 0; i < N; i++) {
41 auto stu = students[i];
42 if(stu->matched_company == nullptr) {
43 flag = true;
44 stu->priority_c[stu->current_priority++]->proposed_students.insert(stu);
45 }
46 }
47
48 for(int i = 0; i < N; i++) {
49 auto com = companies[i];
50 Student *current_matched_student = com->matched_student;
51 if(com->proposed_students.size() > 0) {
52 int min_priority = com->matched_student ? com->priority[com->matched_student] : INT_MAX;
53 Student *max_p_student = com->matched_student;
54 for(auto stu: com->proposed_students) {
55 if(com->priority[stu] < min_priority) {
56 min_priority = com->priority[stu];
57 max_p_student = stu;
58 flag = true;
59 }
60 }
61 if(flag) {
62 if(current_matched_student != nullptr) {
63 current_matched_student->matched_company = nullptr;
64 }
65 com->matched_student = max_p_student;
66 max_p_student->matched_company = com;
67 }
68 com->proposed_students.clear();
69 }
70 }
71 }
72 vector<int> ans = vector<int>(N, -1);
73 for(int i = 0; i < N; i++) {
74 ans[i] = students[i]->matched_company->id;
75 }
76 return ans;
77 }
78 }// namespace stable_matching
79
80 namespace oddecho {
81 int main(istream &cin, ostream &cout) {
82 string str;
83 int cnt = 0;
84 while(cin >> str) {
85 if(cnt % 2 == 1) {
86 cout << str << endl;
87 }
88 cnt++;
89 }
90 return 0;
91 }
92 }// namespace oddecho
93
94 namespace recount {
95 int main(istream &cin, ostream &cout) {
96 unordered_map<string, unsigned long> m = unordered_map<string, unsigned long>();
97 string line;
98 while(std::getline(cin, line)) {
99 if(line == "***") {
100 break;
101 }
102 m[line]++;
103 }
104
105 string ans = "***";
106 unsigned long max_vote = 0;
107 for(const auto &[k, v]: m) {
108 max_vote = max(max_vote, v);
109 }
110 for(const auto &[k, v]: m) {
111 if(v == max_vote) {
112 if(ans != "***") {
113 cout << "Runoff!";
114 return 0;
115 }
116 ans = k;
117 }
118 }
119 cout << ans;
120
121 return 0;
122 }
123 }// namespace recount
124
125 namespace set {
126 vector<vector<int>> ans = {};
128 this->cards.insert(c);
129 this->cnt = 1;
130 }
131
132 unsigned short calc_mask(card *c1, card *c2) {
133 unsigned short mask = 0;
134 for(int i = 0; i < 4; i++) {
135 mask <<= 1;
136 mask |= c1->f[i] == c2->f[i];
137 }
138 return mask;
139 }
140 card::card(int id, string s) {
141 this->id = id;
142 istringstream iss(s);
143 iss >> this->f[0] >> this->f[1] >> this->f[2] >> this->f[3];
144 }
145
147 if(this->mask == (unsigned short) (-1)) {
148 this->mask = calc_mask(c, *this->cards.begin());
149 }
150 this->cards.insert(c);
151 this->cnt++;
152 if(this->cnt == 3) {
153 vector<int> vec = {};
154 for(auto &c: this->cards) {
155 vec.emplace_back(c->id);
156 }
157 std::sort(vec.begin(), vec.end());
158 ans.emplace_back(vec);
159 }
160 }
161
162 bool fit(card *c, const cardset *s) {
163 if(s->mask == (unsigned short) (-1)) {
164 return true;
165 }
166 for(auto &sc: s->cards) {
167 if(calc_mask(sc, c) != s->mask) {
168 return false;
169 }
170 }
171 return true;
172 }
173
174 int main(istream &cin, ostream &cout) {
175 cardset sets[1 << 10] = {};
176 int sets_cnt = 0;
177 string input;
178 for(int i = 1; i <= 12; i++) {
179 cin >> input;
180 card *newcard = new card(i, input);
181 for(int j = 0; j < sets_cnt; j++) {
182 if(fit(newcard, &sets[j])) {
183 cardset newset = sets[j];
184 newset.insert(newcard);
185 sets[sets_cnt++] = (newset);
186 }
187 }
188 cardset newset = cardset(newcard);
189 sets[sets_cnt++] = (newset);
190 }
191 if(ans.size() == 0) {
192 cout << "no sets";
193 return 0;
194 }
195 std::sort(ans.begin(), ans.end(), [](const vector<int> &a, const vector<int> &b) {
196 if(a[0] != b[0]) {
197 return a[0] < b[0];
198 } else if(a[1] != b[1]) {
199 return a[1] < b[1];
200 } else {
201 return a[2] < b[2];
202 }
203 });
204 for(const auto &s: ans) {
205 cout << s[0] << ' ' << s[1] << ' ' << s[2] << endl;
206 }
207 return 0;
208 }
209 }// namespace set
210
211 namespace plantingtrees {
212 int main(istream &cin, ostream &cout) {
213 int n;
214 cin >> n;
215 vector<int> vec(n);
216 for(int i = 0; i < n; i++) {
217 cin >> vec[i];
218 }
219 sort(vec.rbegin(), vec.rend());
220 int ans = 0;
221 for(int i = 0; i < n; i++) {
222 ans = max(ans, i + vec[i] + 2);
223 }
224 cout << ans;
225
226 return 0;
227 }
228 }// namespace plantingtrees
229
230 namespace snowflakes {
231 int main(istream &cin, ostream &cout) {
232 int testCases;
233 cin >> testCases;
234 while(testCases--) {
235 int n;
236 cin >> n;
237 if(n == 0) {
238 cout << 0 << endl;
239 break;
240 }
241 vector<int> snowflakes(n);
242 for(int i = 0; i < n; ++i) {
243 cin >> snowflakes[i];
244 }
245 unordered_map<int, int> lastSeen;
246 int left = 0;
247 int maxLength = 0;
248 for(int right = 0; right < n; ++right) {
249 int currentSnowflake = snowflakes[right];
250 if(lastSeen.count(currentSnowflake)) {
251 left = max(left, lastSeen[currentSnowflake] + 1);
252 }
253 lastSeen[currentSnowflake] = right;
254 maxLength = max(maxLength, right - left + 1);
255 }
256 cout << maxLength;
257 }
258
259 return 0;
260 }
261 }// namespace snowflakes
262
263 namespace amalgram {
264 int main(istream &cin, ostream &cout) {
265 string a, b;
266 cin >> a >> b;
267 vector<int> cnt1 = vector<int>(26, 0);
268 vector<int> cnt2 = vector<int>(26, 0);
269 for(char c: a) {
270 cnt1[c - 'a']++;
271 }
272 for(char c: b) {
273 cnt2[c - 'a']++;
274 }
275 for(int i = 0; i < 26; i++) {
276 for(int j = 0; j < max(cnt1[i], cnt2[i]); j++) {
277 cout << (char) ('a' + i);
278 }
279 }
280 return 0;
281 }
282 }// namespace amalgram
283}// namespace comp526
int main(int argc, char **argv)
定义 main.cpp:5
int main(istream &cin, ostream &cout)
int main(istream &cin, ostream &cout)
bool fit(card *c, const cardset *s)
unsigned short calc_mask(card *c1, card *c2)
vector< vector< int > > ans
int main(istream &cin, ostream &cout)
unordered_map< int, Company * > priority_c
unordered_map< Company *, int > c_priority
unordered_map< Student *, int > priority
vector< int > match(vector< vector< int > >, vector< vector< int > >)
card(int id, string s)
unordered_set< card * > cards
unsigned short mask