/* VNIMANIYE! Etot fail rashiryet prototipy. Zapreshaetsa ispolzovat sovmestno s drugimi failami. */ process.stdin.resume(); process.stdin.setEncoding('utf8'); Array.prototype.$ = function (f) { return f(this) } Array.prototype.sort1 = function (f) { this.sort(f); return this } function etoKusochekBaytika(kus) { return kus.length !== 0 && kus.replace(/0|1/g, '').length == 0 } function monetki(n, nominals) { return nominals.map(function (nom) { if (nom < n) { return monetki(n - nom, nominals).map(function (tail) { return [nom].concat(tail); }) } else if (nom == n) { return [[nom]] } else { return []; } }).reduce(function (r, a) { return r.concat(a) }, []) } function mprocess(monetki) { var dict = {}, res = [], i monetki.forEach(function (m) { m.sort(function (x,y) { return y-x}) dict[m] = m }) for(i in dict) { res.push(dict[i]) } res.sort(function (x,y) { return y.length - x.length}) return res } function sdelatBaytiki(kusochki) { var baitiki = [] , kluchiki = function (slovar) { var kl=[], i; for (i in slovar) { kl.push(i) }; return kl; } , stopochki = kusochki.reduce(function (stopki, kus) { stopki[kus.length] = (stopki[kus.length] || []).concat([kus]) return stopki }, {}) , ostalos = function (stopki) { return kluchiki(stopki).reduce(function (x,y) { return x+stopki[y].length }, 0) } , try_match = function (stopki, schemka) { var rollback = function (baitik) { baitik.forEach(function (kus) { stopki[kus.length].push(kus) }) } return schemka.reduce(function (baitik, ks) { if (baitik === null) return null if (stopki[ks] && stopki[ks].length > 0) { return [stopki[ks].pop()].concat(baitik) } else { rollback(baitik); return null; } }, []) } , schemki = mprocess(monetki(8, kluchiki(stopochki).map(Number))) , schemka = schemki.pop() , baitik = null; console.log(schemki) while (schemka && ostalos(stopochki) > 0) { while(schemka && !(baitik = try_match(stopochki, schemka))) { schemka = schemki.pop() } if (baitik !== null) baitiki.push(baitik) } console.log('Ostalos', ostalos(stopochki)) return baitiki } function pechatatBaytik(bayt) { process.stdout.write(bayt.join(' + ') + '\n') } function sobratBaytiki(zemlya) { zemlya .split(/\s+/) .filter(etoKusochekBaytika) .sort1(function (x, y) { return y.length - x.length }) .$(sdelatBaytiki) .forEach(pechatatBaytik) } process.stdin.on('data', sobratBaytiki)
11 01 1 1 0000 00 01 000 X 1010 111 010 0001 00 10 001 000 X 0 1100 0 010 X 10 00100 110 1011 000 1100 01101 1100 X 0000 01 1 0001 01 10 10 00100 110 1011
[ [ 1, 1, 1, 1, 1, 1, 1, 1 ], [ 2, 1, 1, 1, 1, 1, 1 ], [ 3, 1, 1, 1, 1, 1 ], [ 2, 2, 1, 1, 1, 1 ], [ 4, 1, 1, 1, 1 ], [ 3, 2, 1, 1, 1 ], [ 2, 2, 2, 1, 1 ], [ 5, 1, 1, 1 ], [ 4, 2, 1, 1 ], [ 3, 3, 1, 1 ], [ 3, 2, 2, 1 ], [ 2, 2, 2, 2 ], [ 5, 2, 1 ], [ 4, 3, 1 ], [ 4, 2, 2 ], [ 3, 3, 2 ], [ 5, 3 ] ] Ostalos 5 0001 + 1011 1100 + 0000 1011 + 1100 0001 + 1100 0000 + 1010 110 + 00100 000 + 01101 110 + 00100 10 + 000 + 010 10 + 010 + 001 01 + 000 + 111 00 + 10 + 10 + 01 11 + 01 + 00 + 01