Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File
- import java.lang.Exception
- import java.lang.StringBuilder
- import java.util.*
- fun loadDict(): Set<String> {
- val dict = mutableSetOf<String>()
- Scanner(File("/home/alexander/russian-dictionary.txt").inputStream()).use {
- while (it.hasNextLine())
- dict.add(it.nextLine())
- }
- return dict
- }
- fun String.shift(shift: Int, alphabet: List<Char>) = StringBuilder().
- apply {
- for (c in this@shift)
- append(alphabet[(alphabet.indexOf(c) + shift) % alphabet.size])
- }.toString()
- fun task111() {
- var decodedSuccessfully = 0
- var total = 0
- val dict = loadDict()
- for (w in dict) {
- val word = w.toLowerCase()
- if (!word.all { alphabet.contains(it) }) continue
- total++
- var cnt = 0
- for (shift in 0 until alphabet.size)
- if (word.shift(shift, alphabet) in dict)
- cnt++
- if (cnt == 1)
- decodedSuccessfully++
- }
- println("$total")
- println("Decoded successfully ${(decodedSuccessfully).toDouble() / (total) * 100.0}%")
- }
- fun main() {
- task111()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement