Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Lwt.Infix
- let hist s =
- let h = Array.make 26 0 in
- for i = 0 to String.length s - 1 do
- let j = Char.code s.[i] - Char.code 'a' in
- h.(j) <- h.(j) + 1
- done;
- h
- let two_three s =
- let f (two, three) = function
- | 2 -> 1, three
- | 3 -> two, 1
- | _ -> two, three
- in
- hist s |>
- Array.fold_left f (0, 0)
- let (++) (a, b) (a', b') = a + a', b + b'
- let spot s t =
- let ct = ref 0 in
- let p = ref 0 (* will get overwritten if useful *) in
- for i = 0 to String.length s - 1 do
- if s.[i] <> t.[i] then (
- incr ct;
- p := i
- )
- done;
- if !ct = 1 then
- Some !p
- else
- None
- exception Found of string
- let () = Lwt_main.run (
- Lwt_io.(read_lines stdin) |> Lwt_stream.to_list >>= fun ids ->
- let two, three = List.map two_three ids |> List.fold_left (++) (0, 0) in
- Lwt_io.printf "%d\n" (two * three) >>= fun () ->
- match
- ids |> List.iter @@ fun s ->
- ids |> List.iter @@ fun t ->
- match spot s t with
- | None ->
- ()
- | Some p ->
- raise (Found String.(sub s 0 p ^ sub s (p + 1) (length s - p - 1)))
- with
- | () -> Lwt_io.eprintl "meh"
- | exception Found s -> Lwt_io.printl s
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement