poj 2503 快排+二分

var x,y:array[0..110000] 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)>>1];
 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:=0;
 while true do
  begin
  readln(s);if s='' then break;
  inc(n);
  y[n]:=copy(s,1,pos(' ',s)-1);
  delete(s,1,pos(' ',s));
  x[n]:=s;
  end;
 qsort(1,n);
 end;
procedure main;
 begin
 while true do
  begin
  readln(s);if s='' then break;
  l:=1;r:=n;
  while l<r do
   begin
   mid:=(l+r)>>1;
   if s<x[mid] then r:=mid
   else if s>x[mid] then l:=mid+1
   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.    
View Code

 

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

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

var x,y:array[0..110000] 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)>>1];
 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:=0;
 while true do
  begin
  readln(s);if s='' then break;
  inc(n);
  y[n]:=copy(s,1,pos(' ',s)-1);
  delete(s,1,pos(' ',s));
  x[n]:=s;
  end;
 qsort(1,n);
 end;
procedure main;
 begin
 while true do
  begin
  readln(s);if s='' then break;
  l:=1;r:=n;
  while l<r do
   begin
   mid:=(l+r)>>1;
   if s<x[mid] then r:=mid
   else if s>x[mid] then l:=mid+1
   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.    
posted @ 2014-06-06 09:17  ZYF-ZYF  Views(166)  Comments(0Edit  收藏  举报