import scala.annotation.tailrec object Solution { @tailrec def countvalleys(w:List[Char], l:Int, cnt:Int):Int = w match { case Nil => cnt case x::xs => countvalleys(xs, if (x=='D') l-1 else l+1, if (x=='D' && l == 0) cnt+1 else cnt) } def main(args: Array[String]) { readLine val s = readLine if (s != null) { println(countvalleys(s.toList, 0, 0)) } } }