题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner fun main(args: Array<String>) { fun max(a: Int, b: Int) = Math.max(a, b) val read = Scanner(System.`in`) val res1 = read.nextLine() val res2 = read.nextLine() val l :String val s : String if (res1.length > res2.length) { l = res1 s = res2 } else { l = res2 s = res1 } var n = 0 for (i in s.indices) { for (j in s.length downTo i + 1) { if(l.contains(s.substring(i, j))) { n = max(j - i, n) break } } } println(n) }