Guest User

Untitled

a guest
Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. lcs([ H|L1],[ H|L2],[H|Lcs]) :- !,
  2. lcs(L1,L2,Lcs).
  3. lcs([H1|L1],[H2|L2],Lcs):-
  4. lcs( L1 ,[H2|L2],Lcs1),
  5. lcs([H1|L1], L2 ,Lcs2),
  6. longest(Lcs1,Lcs2,Lcs),!.
  7. lcs(_,_,[]).
  8.  
  9. longest(L1,L2,Longest) :-
  10. length(L1,Length1),
  11. length(L2,Length2),
  12. ((Length1 > Length2) -> Longest = L1; Longest = L2).
  13.  
  14. :- set_prolog_flag(double_quotes, chars). % "abc" = [a,b,c]
  15.  
  16. prefix_of(Prefix, List) :-
  17. append(Prefix, _, List).
  18.  
  19. commonprefix(Prefix, Lists) :-
  20. maplist(prefix_of(Prefix), Lists).
  21.  
  22. ?- commonprefix(Prefix, ["interview", "integrate", "intermediate"]).
  23. Prefix = []
  24. ; Prefix = "i"
  25. ; Prefix = "in"
  26. ; Prefix = "int"
  27. ; Prefix = "inte"
  28. ; false.
  29.  
  30. ?- commonprefix(Prefix, ["interview", "integrate", Xs]).
  31. Prefix = []
  32. ; Prefix = "i", Xs = [i|_A]
  33. ; Prefix = "in", Xs = [i, n|_A]
  34. ; Prefix = "int", Xs = [i, n, t|_A]
  35. ; Prefix = "inte", Xs = [i, n, t, e|_A]
  36. ; false.
  37.  
  38. ?- commonprefix(Prefix, ["interview", "integrate", Xs]), Xs = "induce".
  39. Prefix = [], Xs = "induce"
  40. ; Prefix = "i", Xs = "induce"
  41. ; Prefix = "in", Xs = "induce"
  42. ; false.
  43.  
  44. ?- Xs = "induce", commonprefix(Prefix, ["interview", "integrate", Xs]).
  45. Xs = "induce", Prefix = []
  46. ; Xs = "induce", Prefix = "i"
  47. ; Xs = "induce", Prefix = "in"
  48. ; false.
  49.  
  50. maxprefix(Prefix, Lists) :-
  51. iwhen(ground(Lists), maxprefix_g(Prefix, Lists)).
  52.  
  53. maxprefix_g(Prefix, Lists_g) :-
  54. setof(N-IPrefix, ( commonprefix(IPrefix, Lists_g), length(IPrefix, N ) ), Ns),
  55. append(_,[N-Prefix], Ns). % the longest one
  56.  
  57. :- set_prolog_flag(double_quotes, chars).
  58.  
  59. longest_common_prefix([], []).
  60. longest_common_prefix([H], H).
  61. longest_common_prefix([H1,H2|T], P) :-
  62. maplist(append(P), L, [H1,H2|T]),
  63. ( one_empty_head(L)
  64. ; maplist(head, L, Hs),
  65. not_all_equal(Hs)
  66. ).
  67.  
  68. one_empty_head([[]|_]).
  69. one_empty_head([[_|_]|T]) :-
  70. one_empty_head(T).
  71.  
  72. head([H|_], H).
  73.  
  74. not_all_equal(L) :-
  75. ( member(H1, L), member(H2, L), H1 = H2 -> true
  76. ; list_to_set(L, S),
  77. not_all_equal_(S)
  78. ).
  79.  
  80. not_all_equal_([H|T]) :-
  81. ( member(H1, T), dif(H, H1)
  82. ; not_all_equal_(T)
  83. ).
  84.  
  85. ?- longest_common_prefix(["interview", "interrupt", "integrate", "intermediate"], Z).
  86. Z = [i, n, t, e] ;
  87. false.
  88.  
  89. ?- longest_common_prefix(["interview", "interrupt", X, "intermediate"], "inte").
  90. X = [i, n, t, e] ;
  91. X = [i, n, t, e, _156|_158],
  92. dif(_156, r) ;
  93. false.
  94.  
  95. ?- longest_common_prefix(["interview", "integrate", X], Z).
  96. X = Z, Z = [] ;
  97. X = [_246|_248],
  98. Z = [],
  99. dif(_246, i) ;
  100. X = Z, Z = [i] ;
  101. X = [i, _260|_262],
  102. Z = [i],
  103. dif(_260, n) ;
  104. X = Z, Z = [i, n] ;
  105. X = [i, n, _272|_274],
  106. Z = [i, n],
  107. dif(_272, t) ;
  108. X = Z, Z = [i, n, t] ;
  109. X = [i, n, t, _284|_286],
  110. Z = [i, n, t],
  111. dif(_284, e) ;
  112. X = Z, Z = [i, n, t, e] ;
  113. X = [i, n, t, e, _216|_224],
  114. Z = [i, n, t, e] ;
  115. false.
  116.  
  117. ?- longest_common_prefix([X,Y], "abc").
  118. X = [a, b, c],
  119. Y = [a, b, c|_60] ;
  120. X = [a, b, c, _84|_86],
  121. Y = [a, b, c] ;
  122. X = [a, b, c, _218|_220],
  123. Y = [a, b, c, _242|_244],
  124. dif(_218, _242) ;
  125. false.
  126.  
  127. ?- longest_common_prefix(L, "abc").
  128. L = [[a, b, c]] ;
  129. L = [[a, b, c], [a, b, c|_88]] ;
  130. L = [[a, b, c, _112|_114], [a, b, c]] ;
  131. L = [[a, b, c, _248|_250], [a, b, c, _278|_280]],
  132. dif(_248, _278) ;
  133. L = [[a, b, c], [a, b, c|_76], [a, b, c|_100]] ;
  134. L = [[a, b, c, _130|_132], [a, b, c], [a, b, c|_100]];
Add Comment
Please, Sign In to add comment