いちばん長いしりとり
Posted feedbacks - Scala
素直に書いてみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.io.{BufferedReader, FileInputStream, InputStreamReader}
import scala.collection.immutable.HashMap
class WordchainGame(var p:String) {
var d:List[String] = null
def initialize:WordchainGame = {
def loop(r:BufferedReader):List[String] = r.readLine match {
case null => List[String]()
case l:String => l.split("\t").toList ++ loop(r)
}
d = loop(new BufferedReader(new InputStreamReader(new FileInputStream(p),"Shift_JIS"))).map(_.takeWhile(_ != '*').mkString).filter(_.length > 0)
this
}
def play:List[List[String]] = {
val r:Map[Char,Char] = HashMap(('ァ','ア'),('ィ','イ'),('ゥ','ウ'),('ェ','エ'),('ォ','オ'),('ッ','ツ'),('ャ','ヤ'),('ュ','ユ'),('ョ','ヨ'))
def loop(g:List[String], l:List[String]):List[List[String]] = l match {
case List() => List(g)
case _ =>(
g match {
case List() => l
case _ => {
val t:Char = if (r.contains(g.first.last)) r(g.first.last) else g.first.last
l.filter(_.first == t)
}
}
).flatMap((w) => w.last match {
case 'ン' => List(w::g)
case _ => loop(w::g, l.filter(_ != w))
}
)
}
loop(List[String](), d)
}
}
object WordchainCombination {
def main(args:Array[String]):Unit =
if (args.length != 1)
println("usage: WordchainCombination WORDLIST_FILE")
else
try {
println(new WordchainGame(args.first).initialize.play.sort(_.length > _.length).first.reverse.mkString("->"))
} catch {
case ex => ex.printStackTrace
}
}
|

greentea #9391() Rating5/7=0.71
一番長くしりとりを続けるためのプログラムを書いてください。
また、単語数に対して、計算量がどのように増えていくかも考えて下さい。
なお、単語リストの一例として
http://www.ais.riec.tohoku.ac.jp/lab/wordlist/index-j.htmlで公開されている
http://www.ais.riec.tohoku.ac.jp/lab/wordlist/fam55_40.txtがあります。
ただし、
・一度使った単語は使わないこと(リストに重複がある可能性は考えなくてよい)
・「ん」で終わる単語を使用するか、リスト内にしりとりを続けられる単語がなくなったときに、しりとりは終了する
・一番最初は、好きな単語から初めてもよい
・「一番長くしりとりを続ける」とは、しりとりが終了するまでに使用する単語数が最大になるよう、しりとりの単語を選ぶことをいう
see: 難聴者のための単語了解度試験用単語リスト
[ reply ]