Created
March 18, 2012 15:00
-
-
Save rirakkumya/2074828 to your computer and use it in GitHub Desktop.
最長重複文字列問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maxDupStr(str:String) = { | |
def tails:String => List[String] = { | |
case "" => Nil | |
case x => x :: tails(x.tail) | |
} | |
val sorted = tails(str) sorted | |
val pair = sorted zip sorted.tail | |
val maxPair = { | |
pair map { case (s1,s2) => (s1 zip s2 filter {c => c._1 == c._2} size, s1)} max | |
} | |
maxPair._2 take maxPair._1 | |
} | |
assert(maxDupStr("abcab") == "ab") | |
assert(maxDupStr("abcdefgcdef") == "cdef") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment