% Remember to use the lgrind style

\Head{}
\File{dressing.pas}{2006}{9}{24}{21:41}{5902}
\L{\LB{\K{program}_\V{dressing};}}
\L{\LB{\C{}\{\$mode_objfpc\}\CE{}}}
\L{\LB{\K{uses}_\V{classes},\V{sysutils};}}
\L{\LB{\K{var}_\V{names}:\V{TStringList};}}
\L{\LB{}\Tab{4}{\V{line}:\V{string};}\Tab{18}{\C{}//read_line\CE{}}}
\L{\LB{}\Tab{4}{\V{garment1},\V{garment2}:_\V{longint};_}}
\L{\LB{}\Tab{4}{\V{spacePos}:_\V{longint};}}
\L{\LB{}\Tab{4}{\V{i},\V{j},\V{k},\V{l},\V{m},\V{oldlen},\V{current}:\V{longint};}}
\L{\LB{}\Tab{4}{\C{}//adjListNeed[i][j]=k,_iff_i_needs_k\CE{}}}
\L{\LB{}\Tab{4}{\V{adjListNeed}:\K{array}_\K{of}_\K{array}_\K{of}_\V{longint};_}}
\L{\LB{}\Tab{4}{\C{}//adjListAllow[i][j]=k,_iff_k_needs_i\CE{}}}
\L{\LB{}\Tab{4}{\V{adjListAllow}:\K{array}_\K{of}_\K{array}_\K{of}_\V{longint};}}
\L{\LB{}\Tab{4}{\V{canBeUsedNow}:_\V{boolean};}}
\L{\LB{}\Tab{4}{}}
\L{\LB{}\Tab{4}{\V{singleSequenceIndex}:_\V{longint};_\C{}//index_in_the_next_array\CE{}}}
\L{\LB{}\Tab{4}{\V{singleSequence}:_\K{array}_\K{of}_\V{longint};_\C{}//begin_of_a_sequence\CE{}}}
\L{\LB{}\Tab{4}{\V{singleSequenceReaded}:_\K{array}_\K{of}_\V{boolean};\C{}//is_something_read\CE{}}}
\L{\LB{}\Tab{4}{}}
\L{\LB{}\Tab{2}{\C{}//searches_a_sequence\CE{}}}
\index{generateSingleSequence}\Proc{generateSingleSequence}\L{\LB{}\Tab{2}{\K{procedure}_\V{generateSingleSequence}(\V{pos}:_\V{longint});}}
\L{\LB{}\Tab{2}{\K{var}_\V{i}:\V{longint};}}
\L{\LB{}\Tab{2}{\K{begin}}}
\L{\LB{}\Tab{4}{\K{if}_\V{singleSequenceReaded}[\V{pos}]_\K{then}_\K{exit};_\C{}//already_visited?\CE{}}}
\L{\LB{}\Tab{4}{\V{singleSequenceReaded}[\V{pos}]:=\V{true};}}
\L{\LB{}\Tab{4}{\C{}//check_everything_needed_for_the_current_garment\CE{}}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{1}_\K{to}_\V{adjListNeed}[\V{pos}][\N{0}]_\K{do}_}}
\L{\LB{}\Tab{6}{\V{generateSingleSequence}(\V{adjListNeed}[\V{pos}][\V{i}]);}}
\L{\LB{}\Tab{4}{\C{}//write_it_in_the_list\CE{}}}
\L{\LB{}\Tab{4}{\V{singleSequence}[\V{singleSequenceIndex}]:=\V{pos};}}
\L{\LB{}\Tab{4}{\V{inc}(\V{singleSequenceIndex});}}
\L{\LB{}\Tab{2}{\K{end};}}
\L{\LB{}\Tab{3}{}}
\L{\LB{\C{}//list_of_every_sequence}\Tab{27}{\CE{}}}
\L{\LB{\K{var}_\V{sequences}:_\K{array}_\K{of}_\K{record}}}
\L{\LB{}\Tab{6}{\V{seq}:_\K{array}_\K{of}_\V{longint};_\C{}//begin_of_a_sequence\CE{}}}
\L{\LB{}\Tab{6}{\V{next}:_\K{array}_\K{of}_\V{longint};_\C{}//which_can_be_taken_next\CE{}}}
\L{\LB{}\Tab{6}{\V{used}:_\K{array}_\K{of}_\V{boolean};_\C{}//already_weared\CE{}}}
\L{\LB{}\Tab{4}{\K{end};}}
\L{\LB{}\Tab{3}{}}
\L{\LB{\K{begin}}}
\L{\LB{}\Tab{2}{\C{}//===============read_clothes_(sec.1)================\CE{}}}
\L{\LB{}\Tab{2}{\V{names}:=\V{TStringList.create};}}
\L{\LB{}\Tab{2}{\V{readln}(\V{line});_\V{line}:=\V{line}+\S{}{'}_{'}\SE{};}}
\L{\LB{}\Tab{2}{\K{while}_\V{line}\<\>\S{}{'}{'}\SE{}_\K{do}_\K{begin}}}
\L{\LB{}\Tab{4}{\V{spacePos}:=\V{pos}(\S{}{'}_{'}\SE{},\V{line});}}
\L{\LB{}\Tab{4}{\V{names.add}(\V{copy}(\V{line},\N{1},\V{spacePos}\-\N{1}));}}
\L{\LB{}\Tab{4}{\V{delete}(\V{line},\N{1},\V{spacePos});}}
\L{\LB{}\Tab{4}{\V{line}:=\V{trimleft}(\V{line});}}
\L{\LB{}\Tab{2}{\K{end};}}
\L{\LB{}\Tab{2}{}}
\L{\LB{}\Tab{2}{\C{}//reserve_memory_for_adjacent_list\CE{}}}
\L{\LB{}\Tab{2}{\V{setlength}(\V{adjListNeed},_\V{names.count},_\V{names.count}+\N{1});}}
\L{\LB{}\Tab{2}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}_\V{adjListNeed}[\V{i}][\N{0}]:=\N{0};}}
\L{\LB{}\Tab{2}{\V{setlength}(\V{adjListAllow},_\V{names.count},_\V{names.count}+\N{1});}}
\L{\LB{}\Tab{2}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}_\V{adjListAllow}[\V{i}][\N{0}]:=\N{0};}}
\L{\LB{}\Tab{2}{}}
\L{\LB{}\Tab{2}{\C{}//read_order\CE{}}}
\L{\LB{}\Tab{2}{\V{readln}(\V{line});_}}
\L{\LB{}\Tab{2}{\K{while}_\V{line}\<\>\S{}{'}{'}\SE{}_\K{do}_\K{begin}}}
\L{\LB{}\Tab{4}{\V{spacePos}:=\V{pos}(\S{}{'}_{'}\SE{},\V{line});}}
\L{\LB{}\Tab{4}{\V{garment1}:=\V{names.indexOf}(\V{copy}(\V{line},\N{1},\V{spacePos}\-\N{1}));}}
\L{\LB{}\Tab{4}{\K{if}_\V{garment1}=\-\N{1}_\K{then}_}}
\L{\LB{}\Tab{6}{\V{raise}_\V{exception.create}(\S{}{'}Invalid_Garment_at_line_start{'}\SE{});}}
\L{\LB{}\Tab{4}{\V{delete}(\V{line},\N{1},\V{spacePos});}}
\L{\LB{}\Tab{4}{\V{garment2}:=\V{names.indexOf}(\V{trimleft}(\V{line}));}}
\L{\LB{}\Tab{4}{\K{if}_\V{garment2}=\-\N{1}_\K{then}_}}
\L{\LB{}\Tab{6}{\V{raise}_\V{exception.create}(\S{}{'}Invalid_Garment_at_line_start{'}\SE{});}}
\L{\LB{}\Tab{4}{\C{}//insert_garment1,2_in_graphs\CE{}}}
\L{\LB{}\Tab{4}{\V{inc}(\V{adjListNeed}[\V{garment2}][\N{0}]);}}
\L{\LB{}\Tab{4}{\V{adjListNeed}[\V{garment2}][\V{adjListNeed}[\V{garment2}][\N{0}]]:=\V{garment1};}}
\L{\LB{}\Tab{4}{\V{inc}(\V{adjListAllow}[\V{garment1}][\N{0}]);}}
\L{\LB{}\Tab{4}{\V{adjListAllow}[\V{garment1}][\V{adjListAllow}[\V{garment1}][\N{0}]]:=\V{garment2};}}
\L{\LB{}\Tab{4}{}}
\L{\LB{}\Tab{4}{\V{readln}(\V{line});}}
\L{\LB{}\Tab{2}{\K{end};}}
\L{\LB{}\Tab{2}{}}
\L{\LB{}\Tab{2}{\C{}//=========find_a_single_sequence_(sec._3)============\CE{}}}
\L{\LB{}\Tab{2}{\K{if}_(\V{paramcount}=\N{0})_\K{or}_(\V{paramstr}(\N{1})=\S{}{'}\-\-only\-one{'}\SE{})_\K{then}_\K{begin}}}
\L{\LB{}\Tab{4}{\V{setlength}(\V{singleSequence},\V{names.count});}}
\L{\LB{}\Tab{4}{\V{setlength}(\V{singleSequenceReaded},\V{names.count});}}
\L{\LB{}\Tab{4}{\V{fillchar}(\V{singleSequenceReaded}[\N{0}],\V{sizeof}(\V{singleSequenceReaded}[\N{0}])}}
\L{\LB{}\Tab{40}{*\V{length}(\V{singleSequenceReaded}),\N{0});}}
\L{\LB{}\Tab{4}{\V{singleSequenceIndex}:=\N{0};}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}}}
\L{\LB{}\Tab{6}{\V{generateSingleSequence}(\V{i});}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}}}
\L{\LB{}\Tab{6}{\V{write}(\V{names}[\V{singleSequence}[\V{i}]],\S{}{'}_{'}\SE{});}}
\L{\LB{}\Tab{4}{\V{writeln}();}}
\L{\LB{}\Tab{2}{\K{end};}}
\L{\LB{}\Tab{2}{}}
\L{\LB{}\Tab{3}{\C{}//============find_all_sequences_(sec._2)============\CE{}}}
\L{\LB{}\Tab{2}{\K{if}_(\V{paramcount}=\N{0})_\K{or}_(\V{paramstr}(\N{1})=\S{}{'}\-\-only-all{'}\SE{})_\K{then}_\K{begin}}}
\L{\LB{}\Tab{4}{\K{if}_\V{paramcount}=\N{0}_\K{then}_\V{writeln};}}
\L{\LB{}\Tab{4}{\C{}//init\CE{}}}
\L{\LB{}\Tab{4}{\V{setlength}(\V{sequences},\N{1});}}
\L{\LB{}\Tab{4}{\K{with}_\V{sequences}[\N{0}]_\K{do}_\K{begin}}}
\L{\LB{}\Tab{6}{\V{setlength}(\V{seq},\V{names.count});}}
\L{\LB{}\Tab{6}{\V{setlength}(\V{next},\N{0});}}
\L{\LB{}\Tab{6}{\V{setlength}(\V{used},\V{names.count});}}
\L{\LB{}\Tab{4}{\K{end};}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}}}
\L{\LB{}\Tab{6}{\K{if}_\V{adjListNeed}[\V{i}][\N{0}]=\N{0}_\K{then}_\K{begin}}}
\L{\LB{}\Tab{8}{\V{setlength}(\V{sequences}[\N{0}]\V{.next},\V{length}(\V{sequences}[\N{0}]\V{.next})+\N{1});}}
\L{\LB{}\Tab{8}{\V{sequences}[\N{0}]\V{.next}[\V{high}(\V{sequences}[\N{0}]\V{.next})]:=\V{i};}}
\L{\LB{}\Tab{6}{\K{end};}}
\L{\LB{}\Tab{4}{\C{}//search\CE{}}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}_\K{begin}}}
\L{\LB{}\Tab{6}{\C{}//create_a_sequence_for_every_possible_appending\CE{}}}
\L{\LB{}\Tab{6}{\C{}//to_any_known_sequence\CE{}}}
\L{\LB{}\Tab{6}{\K{for}_\V{j}:=\N{0}_\K{to}_\V{high}(\V{sequences})_\K{do}_\K{begin}}}
\L{\LB{}\Tab{8}{\V{oldlen}:=\V{length}(\V{sequences});}}
\L{\LB{}\Tab{8}{\V{setlength}(\V{sequences},\V{length}(\V{sequences})+\V{length}(\V{sequences}[\V{j}]\V{.next})\-\N{1});}}
\L{\LB{}\Tab{8}{\K{for}_\V{k}:=\V{oldlen}_\K{to}_\V{high}(\V{sequences})_\K{do}_\K{begin}}}
\L{\LB{}\Tab{10}{\V{sequences}[\V{k}]:=\V{sequences}[\V{j}];}}
\L{\LB{}\Tab{10}{\C{}//copy_arrays\CE{}}}
\L{\LB{}\Tab{10}{\V{setlength}(\V{sequences}[\V{k}]\V{.seq},\V{length}(\V{sequences}[\V{k}]\V{.seq}));}}
\L{\LB{}\Tab{10}{\V{setlength}(\V{sequences}[\V{k}]\V{.used},\V{length}(\V{sequences}[\V{k}]\V{.used}));}}
\L{\LB{}\Tab{10}{\V{setlength}(\V{sequences}[\V{k}]\V{.next},\V{length}(\V{sequences}[\V{k}]\V{.next}));}}
\L{\LB{}\Tab{10}{}}
\L{\LB{}\Tab{10}{\C{}//add_to_sequence\CE{}}}
\L{\LB{}\Tab{10}{\V{sequences}[\V{k}]\V{.seq}[\V{i}]:=\V{sequences}[\V{j}]\V{.next}[\V{k}\-\V{oldlen}];_}}
\L{\LB{}\Tab{10}{\C{}//remove_from_next\CE{}}}
\L{\LB{}\Tab{10}{\V{sequences}[\V{k}]\V{.next}[\V{k}\-\V{oldlen}]:=\V{sequences}[\V{k}]\V{.next}[\V{high}(\V{sequences}[\V{k}]\V{.next})];}}
\L{\LB{}\Tab{10}{\V{setlength}(\V{sequences}[\V{k}]\V{.next},\V{high}(\V{sequences}[\V{k}]\V{.next}));}}
\L{\LB{}\Tab{8}{\K{end};}}
\L{\LB{}\Tab{8}{\C{}//add_last_possibility_to_current_sequence\CE{}}}
\L{\LB{}\Tab{8}{\V{sequences}[\V{j}]\V{.seq}[\V{i}]:=\V{sequences}[\V{j}]\V{.next}[\V{high}(\V{sequences}[\V{j}]\V{.next})];}}
\L{\LB{}\Tab{8}{\V{setlength}(\V{sequences}[\V{j}]\V{.next},\V{high}(\V{sequences}[\V{j}]\V{.next}));}}
\L{\LB{}\Tab{6}{\K{end};}}
\L{\LB{}}
\L{\LB{}\Tab{6}{\C{}//search_all_new_nodes_that_can_be_used_now\CE{}}}
\L{\LB{}\Tab{6}{\K{for}_\V{j}:=\N{0}_\K{to}_\V{high}(\V{sequences})_\K{do}_\K{begin}}}
\L{\LB{}\Tab{8}{\V{sequences}[\V{j}]\V{.used}[\V{sequences}[\V{j}]\V{.seq}[\V{i}]]:=\V{true};_}}
\L{\LB{}\Tab{8}{\K{for}_\V{k}:=\N{1}_\K{to}_\V{adjListAllow}[\V{sequences}[\V{j}]\V{.seq}[\V{i}]][\N{0}]_\K{do}_\K{begin}}}
\L{\LB{}\Tab{10}{\V{current}:=\V{adjListAllow}[\V{sequences}[\V{j}]\V{.seq}[\V{i}]][\V{k}];}}
\L{\LB{}\Tab{10}{\C{}//current_is_the_only_thing_which_was_earlier_not_allowed,_\CE{}}}
\L{\LB{}\Tab{10}{\C{}//but_can_it_be_now.\CE{}}}
\L{\LB{}\Tab{10}{\C{}//Check_if_current_can_really_used:\CE{}}}
\L{\LB{}\Tab{10}{\V{canBeUsedNow}:=\V{true};}}
\L{\LB{}\Tab{10}{\K{for}_\V{l}:=\N{1}_\K{to}_\V{adjListNeed}[\V{current}][\N{0}]_\K{do}}}
\L{\LB{}\Tab{12}{\K{if}_\K{not}_\V{sequences}[\V{j}]\V{.used}[\V{adjListNeed}[\V{current}][\V{l}]]_\K{then}_\K{begin}}}
\L{\LB{}\Tab{14}{\V{canBeUsedNow}:=\V{false};}}
\L{\LB{}\Tab{14}{\K{break};}}
\L{\LB{}\Tab{12}{\K{end};}}
\L{\LB{}\Tab{10}{\C{}//if_it_can_be_used,_add_to_next\CE{}}}
\L{\LB{}\Tab{10}{\K{if}_\V{canBeUsedNow}_\K{then}_\K{begin}_}}
\L{\LB{}\Tab{12}{\V{setlength}(\V{sequences}[\V{j}]\V{.next},\V{length}(\V{sequences}[\V{j}]\V{.next})+\N{1});}}
\L{\LB{}\Tab{12}{\V{sequences}[\V{j}]\V{.next}[\V{high}(\V{sequences}[\V{j}]\V{.next})]:=\V{current};}}
\L{\LB{}\Tab{10}{\K{end};}}
\L{\LB{}\Tab{8}{\K{end};}}
\L{\LB{}\Tab{6}{\K{end};}}
\L{\LB{}\Tab{4}{\K{end};}}
\L{\LB{}}
\L{\LB{}\Tab{4}{\C{}//print_all_sequences\CE{}}}
\L{\LB{}\Tab{4}{\K{for}_\V{i}:=\N{0}_\K{to}_\V{high}(\V{sequences})_\K{do}_\K{begin}}}
\L{\LB{}\Tab{6}{\K{for}_\V{j}:=\N{0}_\K{to}_\V{names.count}\-\N{1}_\K{do}}}
\L{\LB{}\Tab{8}{\V{write}(\V{names}[\V{sequences}[\V{i}]\V{.seq}[\V{j}]],\S{}{'}_{'}\SE{});}}
\L{\LB{}\Tab{6}{\V{writeln}();}}
\L{\LB{}\Tab{4}{\K{end};}}
\L{\LB{}\Tab{2}{\K{end};}}
\L{\LB{}\Tab{2}{\V{names.free};}}
\L{\LB{\K{end.}}}
