Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val f = readLine()!!
- val s = readLine()!!
- val t = readLine()!!
- var size = 0
- var ans = 0 to 0
- Array(f.length + 1) { Array(s.length + 1) { IntArray(t.length + 1) } }
- .let { dp ->
- (f.indices).forEach { i ->
- (s.indices).forEach { q ->
- (t.indices).forEach { r ->
- when {
- f[i] == s[q] && f[i] == t[r] -> {
- dp[i][q][r] = when {
- i == 0 || q == 0 || r == 0 -> 1
- else -> dp[i - 1][q - 1][r - 1] + 1
- }
- if (dp[i][q][r] >= size) {
- if (dp[i][q][r] > size)
- size = dp[i][q][r]
- ans = i - size + 1 to i + 1
- }
- }
- else -> dp[i][q][r] = 0
- }
- }
- }
- }
- }
- println(f.substring(ans.first, ans.second))
- }
Add Comment
Please, Sign In to add comment