Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.github.contest
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.strings.camelMatch
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
import java.util.TreeMap
Expand All @@ -22,6 +23,7 @@ fun main() {
fun launchPerformance() {

val text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit".repeat(10)
val new = "banana".repeat(5000)
val patterns = listOf(
"Lorem", "ipsum", "dolor", "sit", "amet",
"consectetur", "adipiscing", "elit", "xyz", "abc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,78 @@ fun findRepeatedDnaSequences(s: String): List<String> {
return res
}

/**
* 1044. Longest Duplicate Substring
*/

fun longestDupSubstring(s: String): String {
if (s.length == 2 && s[0] == s[1]) return "${s[0]}"

var left = 1
var right = s.length
var maxLen = 0
var res = ""

while (left < right) {

val window = (left + right) / 2

if (window == maxLen) return res

val check = findDuplicate(s, window)

if (check != null) {
left = window
maxLen = window
res = check
} else right = window
}

return res
}


private fun findDuplicate(s: String, length: Int): String? {
val base = 26
val mod = 1_000_000_007
var hash = 0L
var power = 1L
val seen = HashMap<Long, MutableList<Int>>()


for (i in 0 until length - 1) {
power = (power * base) % mod
}


for (i in 0 until length) {
hash = (hash * base + (s[i] - 'a')) % mod
}
seen.getOrPut(hash) { mutableListOf() }.add(0)


for (i in 1..s.length - length) {

hash = (hash - (s[i - 1] - 'a') * power % mod + mod) % mod

hash = (hash * base + (s[i + length - 1] - 'a')) % mod


if (seen.containsKey(hash)) {
val currentSub = s.substring(i, i + length)
for (start in seen[hash]!!) {
if (s.substring(start, start + length) == currentSub) {
return currentSub
}
}
seen[hash]!!.add(i)
} else {
seen[hash] = mutableListOf(i)
}
}

return null
}



Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ fun rabinKarpMultiPattern(text: String, patterns: List<String>): Map<String, Lis

// Предварительно вычисляем хеши всех образцов
for (pattern in patterns.distinct()) {
val m = pattern.length
if (m == 0 || m > text.length) continue
val patternLen = pattern.length
if (patternLen == 0 || patternLen > text.length) continue

var hash = 0
for (i in 0 until m) {
for (i in 0 until patternLen) {
hash = (d * hash + pattern[i].code) % q
}

Expand Down