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

What's wrong with test#9
Posted by Arthas 30 May 2010 08:41
My program gives a wrong answer with test#9,but I don't know where the mistake is,please help me correct it
program ural1002;
var
  f,w:array[0..100,0..100]of longint;
  s:string;
  n:longint;
  ans:longint;
  c,cc:array[1..50000]of string[51];
  list:array['0'..'9',1..50,0..500]of longint;
const
  func:array['a'..'z']of char
  =('2','2','2','3','3','3','4','4','1','1','5','5',
    '6','6','0','7','0','7','7','8','8','8','9','9','9','0');
procedure init;
var
  i,j,k,l:longint;ss:string[51];
begin
  readln(s);
  if s='-1' then halt;
  readln(n);
  for i:=1 to n do
  begin
    readln(c[i]);
    cc[i]:=c[i];
  end;
  fillchar(list,sizeof(list),0);
  for i:=1 to n do
  begin
    ss:='';
    for j:=1 to length(c[i]) do ss:=ss+func[c[i,j]];
    c[i]:=ss;
    inc(list[c[i,1],length(c[i]),0]);
    list[c[i,1],length(c[i]),list[c[i,1],length(c[i]),0]]:=i;
  end;
  for i:=0 to length(s) do for j:=0 to length(s) do
  w[i,j]:=i;
  for i:=0 to length(s) do for j:=0 to length(s) do if i=j then
  f[i,j]:=0 else f[i,j]:=n+1;
  for i:=1 to length(s) do
  for j:=1 to 50 do
  for k:=1 to list[s[i],j,0] do
  if copy(s,i,j)=c[list[s[i],j,k]] then
  begin
    f[i-1,i+j-1]:=1;
    w[i-1,i+j-1]:=-list[s[i],j,k];
  end;
end;
function dp:longint;
var
  i,j,k:longint;
begin
  for k:=0 to length(s) do
  for i:=0 to k do
  for j:=k to length(s) do
  if f[i,k]+f[k,j]<f[i,j] then
  begin
    f[i,j]:=f[i,k]+f[k,j];
    w[i,j]:=k;
  end;
  dp:=f[0,length(s)];
end;
procedure print(l,r:longint);
begin
  if w[l,r]<0 then
  begin
    write(cc[-w[l,r]]);
    if r<length(s) then write(' ')else writeln;
    exit;
  end
  else
  begin
    print(l,w[l,r]);
    print(w[l,r],r);
  end;
end;
begin
  while true do
  begin
    init;
    ans:=dp;
    if ans<=n then
    print(0,length(s))
    else
    writeln('No solution.');
  end;
end.
Re: What's wrong with test#9
Posted by FriendNew 23 Sep 2010 15:21
I have the same problem,but when I change then range of the string(in yours is [51]),AC immediately.Try it.
Re: What's wrong with test#9
Posted by Andrei Ismail 22 Oct 2011 16:57
I had a problem with test 9 because I was assuming that the same word cannot be repeated (actually I thought there were at most k+1 words in the solution, where k = dictionary size).