English 中文(简体)
log 长期分立
原标题:gprolog longest subset
  • 时间:2011-11-21 20:42:07
  •  标签:
  • prolog

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/2longest Subseq/4

有一个缓冲(目前是单质的分离),时间最长(目前是最长的次长)和一个蓄水器。

在某些行为中,我们需要处理:

  1. Buffer is empty. We put new element in it.
  2. New element is smaller than last buffer element. We put that element in buffer.
  3. New element is larger than last buffer element. We clear buffer and put that element in it. If buffer was larger than longest subsequence, we substitute it.

因此,这似乎是可行的。

?- 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 。





相关问题
Prolog : Learning by example

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 ...

Working with lists in Prolog

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 ...

SWI-Prolog conditional statements

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 ...

prolog cut off in method

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, [...

Max out of values defined by prolog clauses

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? ...

热门标签