let () =
  let s = Scanf.scanf "%s " (fun a -> a) in
  let k = Scanf.scanf "%d " (fun a -> a) in

  let srd l = 
    let sl = List.sort compare l in
    let rec go l acc = match l with
      | [] -> List.rev acc
      | [x] -> List.rev (x::acc) 
      | (x1::x2::xs) -> 
        if x1 = x2
        then go (x2::xs) acc
        else go (x2::xs) (x1::acc)
    in go sl []
  in

  let length = String.length s in

  let rec f str fst fnl res = 
    if fst = length then res else (
      let res = (String.sub str fst fnl) :: res in
      if (fst + fnl = length || fnl - fst = k) then
        f str (fst+1) 1 res
      else
        f str fst (fnl+1) res
    )

  in
  let lst = f s 0 1 [] in
  let lst = srd lst in

  List.iter print_endline lst;
  print_endline (List.nth lst (k-1))

