I have list of integers, I need to find the longest subset of ascending integers from the list.
For example: [1,2,5,3,6,7,4]
- longest subset would have been SS = [1,2,3,6,7]
.
每个人都能至少向我展示实现这一目的的主要指南。
I have list of integers, I need to find the longest subset of ascending integers from the list.
For example: [1,2,5,3,6,7,4]
- longest subset would have been SS = [1,2,3,6,7]
.
每个人都能至少向我展示实现这一目的的主要指南。
longestSubseq( List, Ans ) :-
longestSubseq( List, [], [], Ans ).
longestSubseq( [], Buffer, [], Buffer ) :- !.
longestSubseq( [], _, AnsRevert, Ans ) :-
reverse( AnsRevert, Ans ).
longestSubseq( [H | Tail], [], Longest, Ans ) :-
longestSubseq( Tail, [H], Longest, Ans ).
longestSubseq( [H | Tail], [BufHead | BufTail], Longest, Ans ) :-
BufHead =< H,
longestSubseq( Tail, [H, BufHead | BufTail], Longest, Ans ).
longestSubseq( [H | Tail], Buffer, Longest, Ans ) :-
[BufHead | _ ] = Buffer,
BufHead > H,
length( Longest, LongestLength ),
length( Buffer, BufferLength ),
(
( BufferLength > LongestLength, NewLongest = Buffer )
;
( BufferLength =< LongestLength, NewLongest = Longest )
),
longestSubseq( Tail, [H], NewLongest, Ans ).
我并不十分熟悉道歉,因此它带有一种血清法。
我们现在有2个基点:longest Subseq/2
和longest Subseq/4
。
在某些行为中,我们需要处理:
因此,这似乎是可行的。
?- longestSubseq( [2], X ).
X = [2] ;
false.
?- longestSubseq( [2,1,2,3,2], X ).
X = [1, 2, 3] ;
false.
收回最后一项内容,检查命令。 element element element passed ;
3. 最佳搜索方法:
% not very tested!
longest(L, S) :-
length(L, Len),
advance(L, Len, [], [], S).
cmp_start([], _).
cmp_start([H|_], E) :- H =< E.
add_to_bag([], P, [P]).
add_to_bag([H|T], P, Bag) :-
P = LenP-_,
H = LenH-_,
( LenH > LenP
-> Bag = [H|Bag1],
add_to_bag(T, P, Bag1)
; Bag = [P, H| T] ).
advance([Len-Start/Tail|Bag], Solution) :-
advance(Tail, Len, Start, Bag, Solution).
advance([], _, Start, _, Solution) :-
reverse(Start, Solution).
advance([H|Tail], Len, Start, Bag, Solution) :-
Len1 is Len - 1,
add_to_bag(Bag, Len1-Start/Tail, Bag1),
( cmp_start(Start, H)
-> advance(Tail, Len, [H|Start], Bag1, Solution)
; advance(Bag1, Solution) ).
部分解决办法作为<代码>。 MaximunLength-StartList/ListOfItems ToBeConsidered 。
I ve read about it in a book but it wasn t explained at all. I also never saw it in a program. Is part of Prolog syntax? What s it for? Do you use it?
I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If ...
First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two ...
Q. Given [1,2,3] in Prolog get back [6,5,3] by reverse accumulation I have the start code: accumalate([H],[H]). accumalate([H1 | H2], [Hnew, H2]), Hnew is H1 + H2. .... I am looking for ...
I m trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don t want it to say "true", i want it to say : "yes, the word hello is contained ...
How to determine whether two list have same element in prolog? If i have two list A and B, i want to know whether they have the same element.
I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...
I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? ...