Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private val charRange = ('a'..'z')
- private const val limit = 1000
- private const val a = 1000
- private const val m = 123_987_123
- fun main() {
- try {
- val firstString = "ckfiar".also { println(it) }
- val firstHash = firstString.polynomialHash(a, m)
- val sb = StringBuilder()
- generateSequence {
- val cur = sb.nextSequence().toString()//.also(::println)
- if (cur.length > limit)
- throw Error("последовательность не найдена")
- else
- cur
- }
- .first {
- it.polynomialHash(a, m) == firstHash && it != firstString
- }
- .let(::println)
- } catch (e: Throwable) {
- e.printStackTrace()
- }
- }
- private fun StringBuilder.nextSequence(): StringBuilder {
- if (isEmpty() || all { it == 'z' }) {
- // самое начало или все элементы -- z
- for (i in indices) this[i] = 'a'
- return append('a')
- }
- for (i in lastIndex downTo 0) {
- if (this[i] == 'z') this[i] = 'a'
- else {
- this[i] = this[i].next()
- break
- }
- }
- return this
- }
- private fun Char.next() = ((this.code - 96) % 26 + 97).toChar()
- private fun randomString(size: Int = 20) =
- buildString {
- repeat(size) {
- append(charRange.random())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement