Cod sursa(job #1819065)

Utilizator hostpostNguyen Long hostpost Data 30 noiembrie 2016 09:26:47
Problema Reguli Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.09 kb
Var
  n : longint ;
  a,x : array[0..500000] of int64 ;
  nxt : array[0..500000] of longint ;

Procedure Enter ;
    var i,j : longint ;
    begin
        read(n) ;
        for i := 1 to n do read(x[i]) ;
    end ;

Procedure Build ;
    var i,j :   longint ;
    begin
        nxt[1] := 0 ; j := 0 ;
        for i := 2 to n-1 do
            begin
                while (j > 0) and (a[i] <> a[j+1]) do j := nxt[j] ;
                if (a[i] = a[j+1]) then
                    begin
                        inc(j) ;
                        nxt[i] := j ;
                    end
                else nxt[i] := 0 ;
            end ;
    end ;

Procedure Main ;
    var i,j,cnt,len : longint ;
    begin
        cnt := 0 ;
        for i := 2 to n do a[i-1] := x[i] - x[i-1] ;
        Build ;
        len := (n-1 - nxt[n-1]);
        writeln(len) ;
        for i := 1 to len do writeln(a[i]) ;
    end ;

Begin
    ASSIGN(Input,'REGULI.in') ; reset(input) ;
    ASSIGN(output,'REGULI.out') ; rewrite(output) ;
    enter ;
    Main ;
    CLOSE(input) ; CLOSE(output) ;
End.