paranid5

3_VSHE

Apr 17th, 2021 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.18 KB | None | 0 0
  1. fun main() {
  2.     val f = readLine()!!
  3.     val s = readLine()!!
  4.     val t = readLine()!!
  5.  
  6.     var size = 0
  7.     var ans = 0 to 0
  8.  
  9.     Array(f.length + 1) { Array(s.length + 1) { IntArray(t.length + 1) } }
  10.         .let { dp ->
  11.             (f.indices).forEach { i ->
  12.                 (s.indices).forEach { q ->
  13.                     (t.indices).forEach { r ->
  14.                         when {
  15.                             f[i] == s[q] && f[i] == t[r] -> {
  16.                                 dp[i][q][r] = when {
  17.                                     i == 0 || q == 0 || r == 0 -> 1
  18.                                     else -> dp[i - 1][q - 1][r - 1] + 1
  19.                                 }
  20.  
  21.                                 if (dp[i][q][r] >= size) {
  22.                                     if (dp[i][q][r] > size)
  23.                                         size = dp[i][q][r]
  24.  
  25.                                     ans = i - size + 1 to i + 1
  26.                                 }
  27.                             }
  28.  
  29.                             else -> dp[i][q][r] = 0
  30.                         }
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.  
  36.     println(f.substring(ans.first, ans.second))
  37. }
Add Comment
Please, Sign In to add comment