diff options
Diffstat (limited to 'harmoneff/magicsort.pas')
-rw-r--r-- | harmoneff/magicsort.pas | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/harmoneff/magicsort.pas b/harmoneff/magicsort.pas new file mode 100644 index 0000000..3d04bbe --- /dev/null +++ b/harmoneff/magicsort.pas @@ -0,0 +1,75 @@ +program magicsort; + +uses math; + +var F: Textfile; + common,s: string; + I,J,L: Integer; + lines: array of string; + nums: array of integer; + found: boolean; + +begin + if Paramcount<>1 then + begin + Writeln('usage: '+Paramstr(0)+' imputfile > outputfile'); + halt; + end; + Assign(F,Paramstr(1)); + Reset(F); + J:=0; + if not eof(F) then + begin + readln(F,S); + writeln(S); + end; + while not eof(F) do + begin + readln(F,S); + if S='' then continue; + if J=0 then common:=s; + inc(J); + for I:=1 to min(length(common),length(S)) do + if common[I]<>S[I] then + begin + delete(common,I,length(common)-I+1); + break; + end; + end; + Close(F); + setlength(lines,J); + setlength(nums,J); + J:=0; + Reset(F); + if not eof(F) then + readln(F,S); + while not eof(F) do + begin + Readln(F,S); + if S='' then continue; + lines[J]:=S; + nums[J]:=0; + I:=length(common)+1; + while (I<length(S)) and (S[I] in ['0'..'9']) do + begin + nums[J]:=nums[J]*10 + ord(S[I]) - ord('0'); + inc(I); + end; + inc(J); + end; + Close(F); + L:=-1; + repeat + found:=false; + J:=-1; + For I:=0 to length(nums)-1 do + if ((J=-1) or (nums[I]<nums[J]) or ((nums[I]=nums[J]) and (I<J))) and + ((L=-1) or (nums[I]>nums[L]) or ((nums[I]=nums[L]) and (I>L))) then + begin + J:=I; + found:=true; + end; + L:=J; + if found then writeln(lines[J]); + until not found; +end. |