ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1002. Phone Numbers

WA #1
Posted by Sevala 31 Oct 2009 16:48
My solution
program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type
  t2 = array[0..101] of integer;
var
  arr: t2;
  len, kol, i: integer;
  s, sa: array[0..501] of string;
  ss: string;

function preobr(st: string): string;
var
  l: integer;
  i: integer;
begin
  l := Length(st);
  for i := 1 to l do
  begin
    case st[i] of
      'i': st[i] := '1';
      'j': st[i] := '1'; //1
      'a': st[i] := '2';
      'b': st[i] := '2';
      'c': st[i] := '2'; //2
      'd': st[i] := '3';
      'e': st[i] := '3';
      'f': st[i] := '3'; //3
      'g': st[i] := '4';
      'h': st[i] := '4'; //4
      'k': st[i] := '5';
      'l': st[i] := '5'; //5
      'm': st[i] := '6';
      'n': st[i] := '6'; //6
      'p': st[i] := '7';
      'r': st[i] := '7';
      's': st[i] := '7'; //7
      't': st[i] := '8';
      'u': st[i] := '8';
      'v': st[i] := '8'; //8
      'w': st[i] := '9';
      'x': st[i] := '9';
      'y': st[i] := '9'; //9
      'o': st[i] := '0';
      'q': st[i] := '0';
      'z': st[i] := '0' //0
    else
      st[i] := '0';
    end;
  end;
  preobr := st;
end;

function cmp(ind: Integer; nom: integer): Boolean;
var
  l, j: integer;
begin
  l := Length(s[nom]);
  if (Len < l + ind - 1) then
  begin
    cmp := false;
    exit;
  end;
  for j := 1 to l do
  begin
    if ss[ind + j - 1] <> s[nom][j] then
    begin
      cmp := False;
      Exit;
    end;
  end;
  cmp := true;
end;

function sravn(ind: Integer): t2;
var
  j: integer;
  be, se: t2;
begin
  if ind >= len + 1 then
    exit;

  FillChar(be, 102 * sizeof(integer), 0);
  FillChar(se, 102 * sizeof(integer), 0);
  be[0] := 101;
  for j := 1 to kol do
  begin
    if cmp(ind, j) then
    begin
      se := sravn(ind + length(s[j]));
      Inc(Se[0]);
      if se[0] > 101 then
        continue;
      se[se[0]] := j;
      if se[0] < be[0] then
        be := se;
    end;
  end;
  sravn := be;
end;

procedure ps;
var
  i: integer;
begin
  for i := 1 to kol do
    s[i] := preobr(sa[i]);
  arr := sravn(1);
end;

begin
  while true do
  begin
    Readln(ss);
    len := Length(ss);
    if ss = '-1' then
      break;
    readln(kol);
    for i := 1 to kol do
      Readln(sa[i]);
    ps;

    if (arr[0] <> 0) and (arr[0] <> 101) then
    begin
      for i := arr[0] downto 2 do
        write(sa[arr[i]], ' ');
      write(sa[arr[1]]);
      writeln;
    end
    else
      Writeln('No solution.');
  end;
end.

I don't now, where is my mistake, my program give right answers to my tests. In the procedure ps, program converted strings and solve. In function sravn, I use DP and find right sequence. Take word and compare it with substring. If it do for sting, find best sequence for new substring. Sorry for my poor english.
Я не знаю, в чем моя ошибка, все мои тесты программа проходит. В процедуре ps, конвертируются слова, зачем ищется решение с помощью функции sravn. Она возвращает в нулевом элементе количество потребовавшихся слов и сами слова. Используется динамическое программирование.