-
-
Save sangkeon/34f6825e70327463f469df87a4c64dea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream | |
import java.io.FileOutputStream | |
object Main extends App { | |
val fileName = "A-large-practice" | |
// val fileName = "A-small-practice" | |
// val fileName = "sample" | |
def solve(str:String) : String = { | |
var charMap = str.toList.groupBy(x => x).mapValues(x => x.size) | |
val digitWords = List("ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE") | |
val numberOrders = List(0,2,8,6,4,5,7,9,1,3) | |
var result = "" | |
for(i <- numberOrders) { | |
val digitWord = digitWords(i) | |
val digitWordMap = digitWord.groupBy(x => x).mapValues(x => x.size) | |
val wordNumbers = digitWordMap.map { | |
case (key, value) => (charMap.getOrElse(key, 0) / value) | |
} | |
val wordNumber = wordNumbers.min | |
if(wordNumber > 0) { | |
for(c <- digitWordMap.keys) { | |
charMap = charMap.updated(c, charMap(c) - digitWordMap(c) * wordNumber) | |
} | |
result += ( i.toString * wordNumber) | |
} | |
} | |
result.sorted | |
} | |
Console.setIn(new FileInputStream(fileName + ".in")) | |
Console.setOut(new FileOutputStream(fileName + ".out")) | |
val numCase = Console.readLine().toInt | |
for( i <- 1 to numCase) { | |
val str = Console.readLine() | |
val result = solve(str) | |
Console.println(f"Case #$i: $result") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment