fork download
  1. /* VNIMANIYE! Etot fail rashiryet prototipy. Zapreshaetsa ispolzovat sovmestno
  2. s drugimi failami. */
  3.  
  4. process.stdin.resume();
  5. process.stdin.setEncoding('utf8');
  6.  
  7. Array.prototype.$ = function (f) { return f(this) }
  8.  
  9. Array.prototype.sort1 = function (f) { this.sort(f); return this }
  10.  
  11. function etoKusochekBaytika(kus) { return kus.length !== 0 && kus.replace(/0|1/g, '').length == 0 }
  12.  
  13.  
  14. function monetki (n, nominals) {
  15.  
  16. function monetki_recur(n, nominals) {
  17. return nominals.map(function (nom) {
  18. if (nom < n) {
  19. return monetki(n - nom, nominals).map(function (tail) {
  20. return [nom].concat(tail);
  21. })
  22. } else if (nom == n) {
  23. return [[nom]]
  24. } else {
  25. return [];
  26. }
  27. }).reduce(function (r, a) { return r.concat(a) }, [])
  28. }
  29.  
  30. function mprocess(monetki) {
  31. var dict = {}, res = [], i
  32. monetki.forEach(function (m) {
  33. m.sort(function (x,y) { return y-x})
  34. dict[m] = m
  35. })
  36. for(i in dict) {
  37. res.push(dict[i])
  38. }
  39. res.sort(function (x,y) { return y.length - x.length})
  40. return res
  41. }
  42.  
  43. return mprocess(monetki_recur(n, nominals))
  44. }
  45.  
  46.  
  47.  
  48. function sdelatBaytiki(kusochki) {
  49. var baitiki = []
  50. , kluchiki = function (slovar) {
  51. var kl=[], i; for (i in slovar) { kl.push(i) }; return kl;
  52. }
  53. , stopochki = kusochki.reduce(function (stopki, kus) {
  54. stopki[kus.length] = (stopki[kus.length] || []).concat([kus])
  55. return stopki
  56. }, {})
  57. , ostalos = function (stopki) {
  58. return kluchiki(stopki).reduce(function (x,y) { return x+stopki[y].length }, 0)
  59. }
  60. , try_match = function (stopki, schemka) {
  61. var rollback = function (baitik) {
  62. baitik.forEach(function (kus) {
  63. stopki[kus.length].push(kus)
  64. })
  65. }
  66. return schemka.reduce(function (baitik, ks) {
  67. if (baitik === null)
  68. return null
  69. if (stopki[ks] && stopki[ks].length > 0) {
  70. return [stopki[ks].pop()].concat(baitik)
  71. } else {
  72. rollback(baitik);
  73. return null;
  74. }
  75. }, [])
  76.  
  77. }
  78. , schemki = monetki(8, kluchiki(stopochki).map(Number))
  79. , schemka = schemki.pop()
  80. , baitik = null;
  81.  
  82. while (schemka && ostalos(stopochki) > 0) {
  83. while(schemka && !(baitik = try_match(stopochki, schemka))) {
  84. schemka = schemki.pop()
  85. }
  86. if (baitik !== null)
  87. baitiki.push(baitik)
  88. }
  89.  
  90. // PROVERKA
  91. if (ostalos(stopochki) == 0) {
  92. console.log('VSE HOROSHO')
  93. } else {
  94. console.log('VNIMANIYE, OSHIBKA')
  95. console.log(stopochki)
  96. console.log(monetki(8, kluchiki(stopochki).map(Number)))
  97. }
  98.  
  99. return baitiki
  100. }
  101.  
  102. function pechatatBaytik(bayt) { process.stdout.write(bayt.join(' + ') + '\n') }
  103.  
  104. function sobratBaytiki(zemlya) {
  105. zemlya
  106. .split(/\s+/)
  107. .filter(etoKusochekBaytika)
  108. .sort1(function (x, y) { return y.length - x.length })
  109. .$(sdelatBaytiki)
  110. .forEach(pechatatBaytik)
  111. }
  112.  
  113. var buffer = ""
  114. process.stdin.on('data', function (chunk) { return buffer+=chunk})
  115. process.stdin.on('end', function () {
  116. var i, buffer1=''
  117. for (i =0; i < 10; i++) {
  118. buffer1+=buffer
  119. }
  120. sobratBaytiki(buffer1)
  121. })
  122.  
Success #stdin #stdout 0.09s 36132KB
stdin
Standard input is empty
stdout
VSE HOROSHO