Created
March 15, 2013 16:03
-
-
Save Wizmann/5170978 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
//最长回文串 | |
//有O(n)的Manacher算法,不过好难写。。。 | |
import scala.math; | |
object Palindrome | |
{ | |
def main(args : Array[String]) | |
{ | |
def isPalindrome(x : String): Boolean = | |
{ | |
x equals x.reverse | |
} | |
def Palindrome(x : String): Int = | |
{ | |
val len = x.length | |
val cases = for { | |
i <- (0 to len) | |
j <- (i+1 to len) | |
} yield x substring(i,j) | |
println(cases filter(isPalindrome(_)) filter(_.length>0) mkString(",")) | |
cases filter(isPalindrome(_)) map(_.length) reduceLeft(math.max(_ , _)) | |
} | |
val x = readLine() | |
println(Palindrome(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment