import scala.collection.immutable.IndexedSeq import scala.io.StdIn object Problem273 { def calcPalindromeLen(S: IndexedSeq[Char], start: Int, end: Int): Int = { val surroundLen = ((0 to S.length) takeWhile { i => val startIndex = start - i val endIndex = end + i startIndex >= 0 && endIndex < S.length && S(startIndex) == S(endIndex) }).last (end - start + 1) + surroundLen * 2 } def proc(S: IndexedSeq[Char]): Int = { if(S.length == 2){ return 1 } val evenPalindromesLen = { for { i <- 0 until S.length - 1 if S(i) == S(i + 1) len = calcPalindromeLen(S, i, i + 1) } yield { if (len == S.length) len - 2 else len } } val oddPalindromesLen = { for { i <- 1 until S.length - 1 if S(i - 1) == S(i + 1) len = calcPalindromeLen(S, i - 1, i + 1) } yield { if (len == S.length) len - 2 else len } } val allPalindromesLen = evenPalindromesLen ++ oddPalindromesLen if (allPalindromesLen.isEmpty) 1 else allPalindromesLen.max } def main(args: Array[String]) { val S: IndexedSeq[Char] = StdIn.readLine().toIndexedSeq val result = proc(S) println(result) } }