Guest User

Untitled

a guest
Feb 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace StableMarriage
  5. {
  6. class MainClass
  7. {
  8. public static void Main(string[] args)
  9. {
  10. var men = new List<Man>()
  11. {
  12. new Man('A', new List<char>("IHJGF")),
  13. new Man('B', new List<char>("FHJGI")),
  14. new Man('C', new List<char>("HGJFI")),
  15. new Man('D', new List<char>("HIFJG")),
  16. new Man('E', new List<char>("JFHGI")),
  17. };
  18. var women = new List<Woman>()
  19. {
  20. new Woman('F', new List<char>("BECAD")),
  21. new Woman('G', new List<char>("DECAB")),
  22. new Woman('H', new List<char>("ECDAB")),
  23. new Woman('I', new List<char>("BECDA")),
  24. new Woman('J', new List<char>("DCBAE")),
  25. };
  26.  
  27. var lonelyGuys = new List<Man>(men);
  28.  
  29. // Carry on until there are no lonely guys anymore.
  30. while (lonelyGuys.Count > 0)
  31. {
  32. // Just go trough the womens again and again.
  33. for (int i = 0; i < women.Count; i++)
  34. {
  35. // Create a list of the men, who want the women.
  36. var bachelors = new List<Man>();
  37. // Add the lonely guys, who want to propose to the women.
  38. for (int j = 0; j < lonelyGuys.Count; j++)
  39. {
  40. if (lonelyGuys[j].NextChoiceId == women[i].Id)
  41. {
  42. bachelors.Add(lonelyGuys[j]);
  43. lonelyGuys[j].NextChoiceTried();
  44. }
  45. }
  46.  
  47. if (bachelors.Count > 0)
  48. {
  49. // Give the Husband a chance to prove himself again.
  50. if (women[i].Partner != null)
  51. {
  52. bachelors.Add(women[i].Partner);
  53. lonelyGuys.Add(women[i].Partner);
  54. women[i].Partner = null;
  55. }
  56. // Let the women choose.
  57. women[i].Partner = bachelors[0];
  58. for (int j = 1; j < bachelors.Count; j++)
  59. {
  60. var keepPartner = true;
  61. for (int k = 0; k < women[i].Choices.Count; k++)
  62. {
  63. if (women[i].Choices[k] == women[i].Partner.Id)
  64. {
  65. keepPartner = true;
  66. break;
  67. }
  68. else if (women[i].Choices[k] == bachelors[j].Id)
  69. {
  70. keepPartner = false;
  71. break;
  72. }
  73. }
  74. if (!keepPartner)
  75. {
  76. women[i].Partner = bachelors[j];
  77. }
  78. }
  79. lonelyGuys.Remove(women[i].Partner);
  80. }
  81. }
  82. }
  83.  
  84. // Write the pairs to console
  85. for (int i = 0; i < women.Count; i++)
  86. {
  87. Console.WriteLine(women[i].Id + " : " + women[i].Partner.Id + " " + men.Count);
  88. }
  89. }
  90. }
  91.  
  92. public class Person
  93. {
  94. public List<char> Choices { get; set; }
  95. public char Id { get; set; }
  96.  
  97. public Person(char id, List<char> choices)
  98. {
  99. Id = id;
  100. Choices = choices;
  101. }
  102. }
  103.  
  104. public class Woman : Person
  105. {
  106. public Man Partner { get; set; }
  107.  
  108. public Woman (char id, List<char> choices) : base (id, choices) {}
  109. }
  110.  
  111. public class Man : Person
  112. {
  113. public HashSet<char> ChoicesTried { get; set; } = new HashSet<char>();
  114. public char? NextChoiceId
  115. {
  116. get
  117. {
  118. char? choiceId = null;
  119.  
  120. for (int i = 0; i < Choices.Count; i++)
  121. {
  122. if (!ChoicesTried.Contains(Choices[i]))
  123. {
  124. choiceId = Choices[i];
  125. break;
  126. }
  127. }
  128. return choiceId;
  129. }
  130. }
  131. public Woman Partner { get; set; }
  132.  
  133. public Man (char id, List<char> choices) : base (id, choices) {}
  134.  
  135. public void NextChoiceTried()
  136. {
  137. ChoicesTried.Add((char)NextChoiceId);
  138. }
  139. }
  140. }
Add Comment
Please, Sign In to add comment