Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal enabledelayedexpansion
  3.  
  4.     %== Initialization ==%
  5. set "male= abe bob col dan ed fred gav hal ian jon" ::First whitespace is necessary
  6. set "female= abi bea cath dee eve fay gay hope ivy jan" ::same here...
  7.  
  8.     ::Initialization of pseudo-arrays [Male]
  9. set "cnt=0" & for %%. in (abi, eve, cath, ivy, jan, dee, fay, bea, hope, gay) do (set abe[!cnt!]=%%.&set /a cnt+=1)
  10. set "cnt=0" & for %%. in (cath, hope, abi, dee, eve, fay, bea, jan, ivy, gay) do (set bob[!cnt!]=%%.&set /a cnt+=1)
  11. set "cnt=0" & for %%. in (hope, eve, abi, dee, bea, fay, ivy, gay, cath, jan) do (set col[!cnt!]=%%.&set /a cnt+=1)
  12. set "cnt=0" & for %%. in (ivy, fay, dee, gay, hope, eve, jan, bea, cath, abi) do (set dan[!cnt!]=%%.&set /a cnt+=1)
  13. set "cnt=0" & for %%. in (jan, dee, bea, cath, fay, eve, abi, ivy, hope, gay) do (set ed[!cnt!]=%%.&set /a cnt+=1)
  14. set "cnt=0" & for %%. in (bea, abi, dee, gay, eve, ivy, cath, jan, hope, fay) do (set fred[!cnt!]=%%.&set /a cnt+=1)
  15. set "cnt=0" & for %%. in (gay, eve, ivy, bea, cath, abi, dee, hope, jan, fay) do (set gav[!cnt!]=%%.&set /a cnt+=1)
  16. set "cnt=0" & for %%. in (abi, eve, hope, fay, ivy, cath, jan, bea, gay, dee) do (set hal[!cnt!]=%%.&set /a cnt+=1)
  17. set "cnt=0" & for %%. in (hope, cath, dee, gay, bea, abi, fay, ivy, jan, eve) do (set ian[!cnt!]=%%.&set /a cnt+=1)
  18. set "cnt=0" & for %%. in (abi, fay, jan, gay, eve, bea, dee, cath, ivy, hope) do (set jon[!cnt!]=%%.&set /a cnt+=1)
  19.  
  20.     ::Initialization of pseudo-arrays [Female]
  21. set "cnt=0" & for %%. in (bob, fred, jon, gav, ian, abe, dan, ed, col, hal) do (set abi[!cnt!]=%%.&set /a cnt+=1)
  22. set "cnt=0" & for %%. in (bob, abe, col, fred, gav, dan, ian, ed, jon, hal) do (set bea[!cnt!]=%%.&set /a cnt+=1)
  23. set "cnt=0" & for %%. in (fred, bob, ed, gav, hal, col, ian, abe, dan, jon) do (set cath[!cnt!]=%%.&set /a cnt+=1)
  24. set "cnt=0" & for %%. in (fred, jon, col, abe, ian, hal, gav, dan, bob, ed) do (set dee[!cnt!]=%%.&set /a cnt+=1)
  25. set "cnt=0" & for %%. in (jon, hal, fred, dan, abe, gav, col, ed, ian, bob) do (set eve[!cnt!]=%%.&set /a cnt+=1)
  26. set "cnt=0" & for %%. in (bob, abe, ed, ian, jon, dan, fred, gav, col, hal) do (set fay[!cnt!]=%%.&set /a cnt+=1)
  27. set "cnt=0" & for %%. in (jon, gav, hal, fred, bob, abe, col, ed, dan, ian) do (set gay[!cnt!]=%%.&set /a cnt+=1)
  28. set "cnt=0" & for %%. in (gav, jon, bob, abe, ian, dan, hal, ed, col, fred) do (set hope[!cnt!]=%%.&set /a cnt+=1)
  29. set "cnt=0" & for %%. in (ian, col, hal, gav, fred, bob, abe, ed, jon, dan) do (set ivy[!cnt!]=%%.&set /a cnt+=1)
  30. set "cnt=0" & for %%. in (ed, hal, gav, abe, bob, jon, col, ian, fred, dan) do (set jan[!cnt!]=%%.&set /a cnt+=1)
  31.     %==/Initialization ==%
  32.  
  33. (   %== The main thing ==%
  34. echo.HISTORY:
  35. call :stableMatching
  36. echo.
  37. echo.NEWLYWEDS:
  38. call :display
  39. echo.
  40. call :isStable
  41. echo.
  42. echo.What if ed and hal swapped?
  43. call :swapper ed hal
  44. echo.
  45. echo.NEW-NEWLYWEDS:
  46. call :display
  47. echo.
  48. call :isStable
  49. pause>nul
  50. exit /b 0
  51. )   %==/The main thing ==%
  52.  
  53.     %== The algorithm ==%
  54. :stableMatching
  55.     set "free_men=%male%"       ::The free-men variable
  56.     set "free_women=%female%"   ::The free-women variable
  57.     set nextgirl=0
  58. :thematchloop
  59.     set m=&for %%F in (!free_men!) do (if not defined m set "m=%%F")
  60.     if "!m!"=="" goto :EOF
  61.  
  62.     for /f "tokens=1-2 delims==" %%A in ('set !m![!nextgirl!]') do set "w=%%B"
  63.     set "propo="
  64.     for %%W in (!free_women!) do (
  65.         if "%%W"=="!w!" (
  66.             set propo=TRUE
  67.             set "!w!_=!m!" & set "!m!_=!w!"
  68.             set free_women=!free_women: %w%=!
  69.             set free_men=!free_men: %m%=!
  70.             echo.   !w! ACCEPTED !m!.
  71.         )
  72.     )
  73.     if defined propo (set "nextgirl=0" & goto thematchloop)
  74.  
  75.     for /f "tokens=1-2 delims==" %%A in ('set !w!_') do set "mbef=%%B"
  76.     set "replace=" & for /f "tokens=1-2 delims==" %%R in ('set !w![') do (
  77.         if not defined replace (
  78.             if "%%S"=="!m!" (
  79.                 set replace=TRUE
  80.                 set "!w!_=!m!" & set "!m!_=!w!"
  81.                 set "free_men=!free_men! !mbef!"
  82.                 set "free_men=!free_men: %m%=!"
  83.                 set nextgirl=0
  84.                 echo.   !w! LEFT !mbef!.
  85.                 echo.   !w! ACCEPTED !m!.
  86.             )
  87.             if "%%S"=="!mbef!" (
  88.                 set /a nextgirl+=1
  89.                 set replace=FALSE
  90.             )
  91.         )
  92.     )
  93. goto thematchloop
  94.     %==/The Algorithm ==%
  95.  
  96.     %== Output the Couples ==%
  97. :display
  98. for %%S in (!male!) do echo.    %%S and !%%S_!.
  99. goto :EOF
  100.     %==/Output the Couples ==%
  101.  
  102.     %== Stability Checking ==%
  103. :isStable
  104. for %%M in (!female!) do (
  105.     set "better="
  106.     set "dislike=" & for /f "tokens=1-2 delims==" %%R in ('set %%M[') do (
  107.         if not defined dislike (
  108.             if "%%S"=="!%%M_!" (set dislike=T) else (set "better=!better! %%S")
  109.         )
  110.     )
  111.     for %%X in (!better!) do (
  112.         for /f "tokens=1-2 delims==" %%F in ('set %%X_') do set curr_partner_of_boy=%%G
  113.         set "main_check="
  114.         for /f "tokens=1-2 delims==" %%B in ('set %%X[') do (
  115.             if not defined main_check (
  116.                 if "%%C"=="%%M" (
  117.                     echo.STABILITY = FALSE.
  118.                     echo %%M and %%X would rather be together than their current partners.
  119.                     goto :EOF
  120.                 )
  121.                 if "%%C"=="!curr_partner_of_boy!" set "main_check=CONTINUE"
  122.             )
  123.         )
  124.     )
  125. )
  126. echo.STABILITY = TRUE.
  127. goto :EOF
  128.     %==/Stability Chacking ==%
  129.  
  130.     %== Swapper ==%
  131. :swapper
  132.     set %~1.tmp=!%~1_!
  133.     set %~2.tmp=!%~2_!
  134.     set "%~1_=!%~2.tmp!"
  135.     set "%~2_=!%~1.tmp!"
  136.     set "!%~1.tmp!_=%~2"
  137.     set "!%~2.tmp!_=%~1"
  138.     goto :EOF
  139.     %==/Swapper==%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement