poj 2503 快排+二分

时间:2023-03-09 05:58:07
poj 2503  快排+二分
var x,y:array[..] of string;
i,n,l,r,mid:longint;
s:string;
procedure qsort(h,l:longint);
var i,j:longint;
temp,m:string;
begin
i:=h;j:=l;m:=x[(i+h)>>];
repeat
while x[i]<m do inc(i);
while x[j]>m do dec(j);
if i<=j then
begin
temp:=x[i];x[i]:=x[j];x[j]:=temp;
temp:=y[i];y[i]:=y[j];y[j]:=temp;
inc(i);dec(j);
end;
until i>j;
if i<l then qsort(i,l);
if j>h then qsort(h,j);
end;
procedure init;
begin
n:=;
while true do
begin
readln(s);if s='' then break;
inc(n);
y[n]:=copy(s,,pos(' ',s)-);
delete(s,,pos(' ',s));
x[n]:=s;
end;
qsort(,n);
end;
procedure main;
begin
while true do
begin
readln(s);if s='' then break;
l:=;r:=n;
while l<r do
begin
mid:=(l+r)>>;
if s<x[mid] then r:=mid
else if s>x[mid] then l:=mid+
else begin l:=mid;r:=mid;end;
end;
if x[l]=s then writeln(y[l]) else writeln('eh');
end;
end;
begin
init;
main;
end.

水一道……体验了一把1A的感觉……

还要说的是,这道题的字符串达到了100000,但还是能够直接排序处理,那是因为它的字符串太短了……

var x,y:array[..] of string;
i,n,l,r,mid:longint;
s:string;
procedure qsort(h,l:longint);
var i,j:longint;
temp,m:string;
begin
i:=h;j:=l;m:=x[(i+h)>>];
repeat
while x[i]<m do inc(i);
while x[j]>m do dec(j);
if i<=j then
begin
temp:=x[i];x[i]:=x[j];x[j]:=temp;
temp:=y[i];y[i]:=y[j];y[j]:=temp;
inc(i);dec(j);
end;
until i>j;
if i<l then qsort(i,l);
if j>h then qsort(h,j);
end;
procedure init;
begin
n:=;
while true do
begin
readln(s);if s='' then break;
inc(n);
y[n]:=copy(s,,pos(' ',s)-);
delete(s,,pos(' ',s));
x[n]:=s;
end;
qsort(,n);
end;
procedure main;
begin
while true do
begin
readln(s);if s='' then break;
l:=;r:=n;
while l<r do
begin
mid:=(l+r)>>;
if s<x[mid] then r:=mid
else if s>x[mid] then l:=mid+
else begin l:=mid;r:=mid;end;
end;
if x[l]=s then writeln(y[l]) else writeln('eh');
end;
end;
begin
init;
main;
end.