The instructor assigned assignments to write down answers to questions about stable marriages. The implementation of the GS Algorithm is similar to that on hdu ......
Question: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1522
In a simple way, C ++ uses a queue to store men who are still single. The exit condition is that the queue is empty. As the answer output must be put before men, it is not necessary to define a bres [] to store men's choices.
# Include <cstring>
# Include <stdio. h>
# Include <string>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <memory>
# Include <iostream>
Using namespace std;
Map <string, int> bmap, gmap;
String bname [501], gname [501];
Int bpre [501 [501]; // used to store boys 'prefrence, bpre [I] [j] = k means to boy I, he ranks the girl k in jth place, the girl k is his jth choice.
Int uple [501] [501]; // used to store girls 'prefrence, gre [I] [j] = k means to girl I, she ranks the boy j in the kth place
// (Different from the storing way of bpre [] [], because to boys, they traverse girls from highest to lowest;
// But to girls, they need to quickly get to know one boy's rank)
Int gres [501]; // used to store girls 'result, gres [I] = j means girl I is with boy j
Int bres [501];
Char input [1500];
Int n;
Queue <int> bqueue;
Int main (){
While (~ Scanf ("% d", & n )){
Memset (gres,-1, sizeof gres );
Memset (bres,-1, sizeof bres );
Getchar ();
Bmap. clear ();
Gmap. clear ();
Int gcount = 0, I = 0, j = 0;
Int index = 0;
For (I = 1; I <= n; I ++ ){
Scanf ("% s", input );
Bname [I] = input;
Bmap [input] = I;
Bqueue. push (I );
Bpre [I] [0] = 1; // B [I] [0] store thes ranking number of the girl whom this boy is asking, every boy starts from the 1st girl.
For (j = 1; j <= n; j ++ ){
Scanf ("% s", input );
If (gmap [input] = 0 ){
Index ++;
Gmap [input] = index;
Gname [index] = input;
}
Bpre [I] [j] = gmap [input];
}
}
For (I = 1; I <= n; I ++ ){
Scanf ("% s", input );
Int gindex = gmap [input];
For (j = 1; j <= n; j ++ ){
Scanf ("% s", input );
Uple [gindex] [bmap [input] = j; // store the rank, the smaller, the higher the propriety will be.
}
}
Int bindex = 0, gaskindex = 0;
While (! Bqueue. empty ()){
Bindex = bqueue. front ();
// Cout <"current boy's name:" <bname [bindex] <endl;
Bqueue. pop ();
Gaskindex = bpre [bindex] [bpre [bindex] [0];
If (gres [gaskindex] <0 ){
Gres [gaskindex] = bindex;
Bres [bindex] = gaskindex;
} Else if (uple [gaskindex] [bindex] <uple [gaskindex] [gres [gaskindex]) {
Bqueue. push (gres [gaskindex]);
Bpre [gres [gaskindex] [0] = (bpre [gres [gaskindex] [0] + 1) % (n + 1 );
Gres [gaskindex] = bindex;
Bres [bindex] = gaskindex;
} Else {
Bqueue. push (bindex );
Bpre [bindex] [0] = (bpre [bindex] [0] + 1) % (n + 1 );
}
}
For (I = 1; I <= n; I ++ ){
Cout <bname [I] <''<gname [bres [I] <endl;
}
}
Return 0;
}
I commented out the code and got 1757B, which is a sin ......