Skip to main content
4 of 7
deleted 66 characters in body

Yellow Pages Search

I've written code in Free Pascal based on a contest problem (The contest insists that I have to write with Pascal). It works like charm, but I think it still need some improvements, from memory usage to runtime.

Input Format

First line is n (Number of people in the Yellow Pages) and q (Number of people to search) {1 <= n <= 10000} and {1 <= q <= 10000}

Next n lines are Yellow Pages entries, each line is the name and the phone number, space delimited. The name is uppercase letters with maximum length of 15, while the length of the phone number is exactly 7 digits.

Next q lines are the people to search, also in uppercase letters with maximum length 15.

Output Format

Output is q lines, where each line is the phone number of the people to search

Input Sample

10 5
AARON 8468431
BEN 1573457
GREGORY 1765743
JACK 3746843
JILL 1357891
MICHAEL 1375638
MONET 1357562
SHEILA 1378651
TERRY 8756345
YIN 1781945
GREGORY
YIN
AARON
MONET
JACK

Output Sample

1765743
1781945
8468431
1357562
3746843

My Code

program YellowPages;

type string7 = String[7];

var
  namearray : Array of shortstring;
  numberarray, searcharray : Array of string7;
  input, name, search : shortstring;
  number : string7;
  n, q, iter1, iter2 : word;

procedure SplitInput(input : shortstring; var name : shortstring; var number : string7);
var 
  hitspace : byte;
begin
  hitspace := pos(' ', input);
  name := Copy(input, 1, hitspace - 1);
  number := Copy(input, hitspace + 1, length(shortstring));
end;

begin
  readln(n, q);
  setlength(namearray, n);
  setlength(numberarray, n);
  setlength(searcharray, q);
  for iter1 := 0 to (n - 1) do
  begin
    readln(input);
    SplitInput(input, name, number);
    namearray[iter1] := name;
    numberarray[iter1] := numberarray;
  end;
  for iter1 := 0 to (q - 1) do
  begin
    readln(search);
    for iter2 := 0 to (n - 1) do
    begin
      if namearray[iter2] = search then break;
    end;
    searcharray[iter1] := numberarray[iter2];
  end;
  for iter1 := 0 to (q - 1) do
  begin
    writeln(searcharray[iter1]);
  end;
end.

Improvement 1 : Changed SplitInput() body to use Copy() instead of manual copy.