let hashtbl_incr h key = try incr (Hashtbl.find h key) with Not_found -> Hashtbl.add h key (ref 1);; let total = Hashtbl.create 100003;; let split_iter f s = let rec word f s p q len = if q < len then match s.[q] with | ' ' -> f (String.sub s p (q - p)); space f s (q + 1) len | _ -> word f s p (q + 1) len else f (String.sub s p (q - p)) and space f s q len = if q < len then match s.[q] with | ' ' -> space f s (q + 1) len | _ -> word f s q (q + 1) len in space f s 0 (String.length s);; try while true do split_iter (hashtbl_incr total) (read_line ()) done with End_of_file -> ();; let words = List.sort String.compare (Hashtbl.fold (fun x y z -> x :: z) total []);; List.iter (fun x -> Printf.printf "%s %d\n" x !(Hashtbl.find total x)) words;;