stringの中身は配列っぽいので普通にloopを回したいけど、逆方向のいい感じのがないので畳み込んだ。

implementation

fun solve(s) =
  let
    val SOME (i, _) = CharVector.findi (fn (i, c) => c = #"A") s
    val SOME  j     = CharVector.foldri (fn (i, c, acc) => if acc = NONE andalso c = #"Z" then SOME i else acc) NONE s
  in
    j - i + 1
  end

val SOME s = TextIO.inputLine TextIO.stdIn
val () = print(Int.toString(solve(s)) ^ "\n")