Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lcs([ H|L1],[ H|L2],[H|Lcs]) :- !,
- lcs(L1,L2,Lcs).
- lcs([H1|L1],[H2|L2],Lcs):-
- lcs( L1 ,[H2|L2],Lcs1),
- lcs([H1|L1], L2 ,Lcs2),
- longest(Lcs1,Lcs2,Lcs),!.
- lcs(_,_,[]).
- longest(L1,L2,Longest) :-
- length(L1,Length1),
- length(L2,Length2),
- ((Length1 > Length2) -> Longest = L1; Longest = L2).
- :- set_prolog_flag(double_quotes, chars). % "abc" = [a,b,c]
- prefix_of(Prefix, List) :-
- append(Prefix, _, List).
- commonprefix(Prefix, Lists) :-
- maplist(prefix_of(Prefix), Lists).
- ?- commonprefix(Prefix, ["interview", "integrate", "intermediate"]).
- Prefix = []
- ; Prefix = "i"
- ; Prefix = "in"
- ; Prefix = "int"
- ; Prefix = "inte"
- ; false.
- ?- commonprefix(Prefix, ["interview", "integrate", Xs]).
- Prefix = []
- ; Prefix = "i", Xs = [i|_A]
- ; Prefix = "in", Xs = [i, n|_A]
- ; Prefix = "int", Xs = [i, n, t|_A]
- ; Prefix = "inte", Xs = [i, n, t, e|_A]
- ; false.
- ?- commonprefix(Prefix, ["interview", "integrate", Xs]), Xs = "induce".
- Prefix = [], Xs = "induce"
- ; Prefix = "i", Xs = "induce"
- ; Prefix = "in", Xs = "induce"
- ; false.
- ?- Xs = "induce", commonprefix(Prefix, ["interview", "integrate", Xs]).
- Xs = "induce", Prefix = []
- ; Xs = "induce", Prefix = "i"
- ; Xs = "induce", Prefix = "in"
- ; false.
- maxprefix(Prefix, Lists) :-
- iwhen(ground(Lists), maxprefix_g(Prefix, Lists)).
- maxprefix_g(Prefix, Lists_g) :-
- setof(N-IPrefix, ( commonprefix(IPrefix, Lists_g), length(IPrefix, N ) ), Ns),
- append(_,[N-Prefix], Ns). % the longest one
- :- set_prolog_flag(double_quotes, chars).
- longest_common_prefix([], []).
- longest_common_prefix([H], H).
- longest_common_prefix([H1,H2|T], P) :-
- maplist(append(P), L, [H1,H2|T]),
- ( one_empty_head(L)
- ; maplist(head, L, Hs),
- not_all_equal(Hs)
- ).
- one_empty_head([[]|_]).
- one_empty_head([[_|_]|T]) :-
- one_empty_head(T).
- head([H|_], H).
- not_all_equal(L) :-
- ( member(H1, L), member(H2, L), H1 = H2 -> true
- ; list_to_set(L, S),
- not_all_equal_(S)
- ).
- not_all_equal_([H|T]) :-
- ( member(H1, T), dif(H, H1)
- ; not_all_equal_(T)
- ).
- ?- longest_common_prefix(["interview", "interrupt", "integrate", "intermediate"], Z).
- Z = [i, n, t, e] ;
- false.
- ?- longest_common_prefix(["interview", "interrupt", X, "intermediate"], "inte").
- X = [i, n, t, e] ;
- X = [i, n, t, e, _156|_158],
- dif(_156, r) ;
- false.
- ?- longest_common_prefix(["interview", "integrate", X], Z).
- X = Z, Z = [] ;
- X = [_246|_248],
- Z = [],
- dif(_246, i) ;
- X = Z, Z = [i] ;
- X = [i, _260|_262],
- Z = [i],
- dif(_260, n) ;
- X = Z, Z = [i, n] ;
- X = [i, n, _272|_274],
- Z = [i, n],
- dif(_272, t) ;
- X = Z, Z = [i, n, t] ;
- X = [i, n, t, _284|_286],
- Z = [i, n, t],
- dif(_284, e) ;
- X = Z, Z = [i, n, t, e] ;
- X = [i, n, t, e, _216|_224],
- Z = [i, n, t, e] ;
- false.
- ?- longest_common_prefix([X,Y], "abc").
- X = [a, b, c],
- Y = [a, b, c|_60] ;
- X = [a, b, c, _84|_86],
- Y = [a, b, c] ;
- X = [a, b, c, _218|_220],
- Y = [a, b, c, _242|_244],
- dif(_218, _242) ;
- false.
- ?- longest_common_prefix(L, "abc").
- L = [[a, b, c]] ;
- L = [[a, b, c], [a, b, c|_88]] ;
- L = [[a, b, c, _112|_114], [a, b, c]] ;
- L = [[a, b, c, _248|_250], [a, b, c, _278|_280]],
- dif(_248, _278) ;
- L = [[a, b, c], [a, b, c|_76], [a, b, c|_100]] ;
- L = [[a, b, c, _130|_132], [a, b, c], [a, b, c|_100]];
- …
Add Comment
Please, Sign In to add comment