Created
April 13, 2013 09:49
-
-
Save a-yasui/5377780 to your computer and use it in GitHub Desktop.
たかし君からの問いかけ、その2
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
/** | |
* 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