https://www.acmicpc.net/problem/1296
왜 답이 DAVE 일까? 난 JOHN이 나오는데..
const input = `MERYLOV
5
JOHN
DAVE
STEVE
JOHN
DAVE`.split('\n')
const word = input[0].split('')
const num = Number(input[1])
const teams = [...new Set(input.slice(2))].map(el => el.split(''))
const solution = (word, num, teams) => {
const w = [...new Set(word)]
let wObj = word.reduce( (ac,v) => ({...ac, [v] : ( ac[v] || 0 ) + 1 }), 0)
const r = []
const getCombinations = (arr, selectNumber) => {
const results = [];
if (selectNumber === 1) return arr.map((el) => [wObj[el]]);
arr.forEach((fixed, index, origin) => {
const rest = origin.slice(index + 1);
const combinations = getCombinations(rest, selectNumber - 1);
const attached = combinations.map((el) => [wObj[fixed], ...el]);
results.push(...attached);
});
return results;
}
teams.forEach(el => {
wObj = word.reduce( (ac,v) => ({...ac, [v] : ( ac[v] || 0 ) + 1 }), 0)
w.forEach(v => {
el.forEach(a => {
if(a === v) wObj[v] += 1
})
})
let per = getCombinations(word, 2).map(v => v[0]+v[1]).reduce((a,c)=>a*c, 1) % 100
let name = el.join('')
r.push([name, per])
})
return r.sort().sort((a, b) => b[1] - a[1])
// [ [ 'JOHN', 72 ], [ 'STEVE', 40 ], [ 'DAVE', 4 ] ]
}
console.log(solution(word, num, teams))