Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.21 KB | None | 0 0
  1. open Lwt.Infix
  2.  
  3. let hist s =
  4.   let h = Array.make 26 0 in
  5.   for i = 0 to String.length s - 1 do
  6.     let j = Char.code s.[i] - Char.code 'a' in
  7.     h.(j) <- h.(j) + 1
  8.   done;
  9.   h
  10.  
  11. let two_three s =
  12.   let f (two, three) = function
  13.     | 2 -> 1, three
  14.     | 3 -> two, 1
  15.     | _ -> two, three
  16.   in
  17.   hist s |>
  18.   Array.fold_left f (0, 0)
  19.  
  20. let (++) (a, b) (a', b') = a + a', b + b'
  21.  
  22. let spot s t =
  23.   let ct = ref 0 in
  24.   let p = ref 0 (* will get overwritten if useful *) in
  25.   for i = 0 to String.length s - 1 do
  26.     if s.[i] <> t.[i] then (
  27.       incr ct;
  28.       p := i
  29.     )
  30.   done;
  31.   if !ct = 1 then
  32.     Some !p
  33.   else
  34.     None
  35.  
  36.  
  37. exception Found of string
  38.  
  39. let () = Lwt_main.run (
  40.   Lwt_io.(read_lines stdin) |> Lwt_stream.to_list >>= fun ids ->
  41.   let two, three = List.map two_three ids |> List.fold_left (++) (0, 0) in
  42.   Lwt_io.printf "%d\n" (two * three) >>= fun () ->
  43.   match
  44.     ids |> List.iter @@ fun s ->
  45.       ids |> List.iter @@ fun t ->
  46.         match spot s t with
  47.         | None ->
  48.           ()
  49.         | Some p ->
  50.           raise (Found String.(sub s 0 p ^ sub s (p + 1) (length s - p - 1)))
  51.   with
  52.   | () -> Lwt_io.eprintl "meh"
  53.   | exception Found s -> Lwt_io.printl s
  54. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement