program Inventura; type ptUZEL = ^tUZEL; tUZEL = record pocet : integer; hrany : array[0..9] of ptUZEL; end; tZASOBNIK = record uzel : ptUZEL; idx : integer; end; var i,k : integer; koren : tUZEL; uzel : ptUZEL; c : char; zas : array[0..100] of tZASOBNIK; vrchol : integer; begin for i:=0 to 9 do koren.hrany[i]:=nil; koren.pocet:=0; readln(k); uzel:=@koren; while not eoln do begin { Přidávám čísla do trie } read(c); if c=',' then begin { Čárka odděluje dvě čísla } inc(uzel^.pocet); uzel:=@koren; end else if c in ['0'..'9'] then begin if uzel^.hrany[ord(c)-ord('0')]<>nil then { Buď jdu po hranách } uzel:=uzel^.hrany[ord(c)-ord('0')] else begin { nebo je vytvářím } new(uzel^.hrany[ord(c)-ord('0')]); uzel:=uzel^.hrany[ord(c)-ord('0')]; for i:=0 to 9 do uzel^.hrany[i]:=nil; uzel^.pocet:=0; end; end; end; inc(uzel^.pocet); { Ošetřím poslední slovo } vrchol:=0; zas[0].uzel:=@koren; zas[0].idx:=-1; while vrchol>=0 do begin { Procházím trií do hloubky } inc(zas[vrchol].idx); while (zas[vrchol].idx<=9) and (zas[vrchol].uzel^.hrany[zas[vrchol].idx]=nil) do inc(zas[vrchol].idx); if zas[vrchol].idx<=9 then begin zas[vrchol+1].idx:=-1; zas[vrchol+1].uzel:=zas[vrchol].uzel^.hrany[zas[vrchol].idx]; inc(vrchol); end else begin if zas[vrchol].uzel^.pocet=k then begin for i:=0 to vrchol-1 do { A vypisuji hledané potraviny } write(chr(zas[i].idx+ord('0'))); writeln; end; dispose(zas[vrchol].uzel); dec(vrchol); end; end; end.