Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
たかし君からの問いかけ、その2
/**
* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
*
* 回文問題
*
* #scala_kansai_beginners
*/
var buff = List(
"apple",
"yellow",
"georgia",
"king",
"email",
"lucky",
"wink", "aaaaa")
/** 一つのながい回文にして、頭と尻を比較して判定 */
var loopword = loop_words(buff)
//println(loopword)
println(if (loopword.length != 0 && loopword.last == loopword.head) "OK" else "NG")
// wordが頭に合うの探して、単語を返す
def findWord (list:List[String], word:Char): String = {
if (list.length == 0) return ""
return if (list.head.head == word) list.head
else findWord(list.tail, word)
}
// 一つ目の要素を長い回文にする
def loop_words (list:List[String]):String = {
if (list.length == 1) return list.head
var word = findWord(list.tail, list.head.last)
var words = list.head + word
return if (word != "")
loop_words(List.concat(List(words),list.tail.filter(fn => fn != word)))
else
""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment