## 10029 - Edit Step Ladders

Moderator: Board moderators

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

### 10029 - Edit Step Ladders

What does it mean:
"x can be transformed to y by adding, deleting, or changing one letter".
Is it an exclusive or or do I have to take into account transformations from dog to go?
Is there any other trick in this problem?
I always get Wrong Answer and don't know why.

gvcormac
Problemsetter & Reviewer
Posts: 194
Joined: Fri Mar 15, 2002 2:00 am
Contact:
Do you get the correct output for the sample? Did you read the question really carefully?

You are barking up the wrong tree looking for a weird interpretation of "edit step." "change a letter"
means "replace a letter by any other letter."

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
Thank you. I was asking, because I had tried to find a mistake in my program very long, and I wanted to be sure that I understood the problem right. Now I found the mistake (it was in the removing part).

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia
Can anybody say why this program gets Runtime Error (Invalid Memory Reference)

[pascal]Program p10029;

Const MaxN = 25000;

Var Voc : Array[1..MaxN]of Record
W : String;
l,max : Integer;
End;
i,N : Integer;

Function FindWord(Var S : String) : Integer;
Var l,r,m : Integer;
begin
l:=1;
r:=N;
While r-l>1 Do Begin
m:=(l+r) div 2;
if Voc[m].W>S then r:=m else l:=m;
End;
if m>1 then if Voc[m-1].W=S then begin FindWord:=m-1; Exit; end;
if Voc[m].W=S then begin FindWord:=m; Exit; end;
if m<N then if Voc[m+1].W=S then begin FindWord:=m+1; Exit; end;
FindWord:=-1;
end;

Procedure SolveFor;
Var S,Snew : String;
i,j : Integer;
Ch : Char;
begin
S:=Voc[N+1].W;
(* try to change *)
for i:=1 to length(S) do
for Ch:='a' to 'z' do begin
Snew:=S;
Snew:=Ch;
j:=FindWord(Snew);
if j<>-1 then
if Voc[j].l+1>Voc[N+1].l then
Voc[N+1].l:=Voc[j].l+1;
end;
(* try to delete *)
for i:=1 to length(S) do begin
Snew:=S;
Delete(Snew,i,1);
if Snew='' then Break;
j:=FindWord(Snew);
if j<>-1 then
if Voc[j].l+1>Voc[N+1].l then
Voc[N+1].l:=Voc[j].l+1;
end;
for i:=1 to length(S)+1 do
for Ch:='a' to 'z' do begin
if i>1 then Snew:=Copy(S,1,i-1) else Snew:='';
Snew:=Snew+Ch;
if i<=length(S) then Snew:=Snew+Copy(S,i,length(S)-i+1);
j:=FindWord(Snew);
if j<>-1 then
if Voc[j].l+1>Voc[N+1].l then
Voc[N+1].l:=Voc[j].l+1;
end;
end;

begin
N:=0;
While Not Eof(InPut) Do begin
Voc[N+1].l:=1;
if N+1>1 then Voc[N+1].max:=Voc[N].max else Voc[N+1].max:=1;
SolveFor;
if Voc[N+1].l>Voc[N+1].max then Voc[N+1].max:=Voc[N+1].l;
N:=N+1;
end;
Writeln(Voc[N].max);
end.[/pascal]

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

### 10029 - Edit Step Ladders

What is a good approach to solve this problem?

I've thought of two, but each got TLE...

At the first one, I had done the LIS algorithm, checking everytime if a word is a edit step of another.

In the second attemp I generate all the possible edit steps for each word read and did a binary search on the list.

So, can anyone help me with this problem?

Jo

junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore
I haven't solved this problem yet but... what is the complexity of your LIS? I know there's a O(n log n) method, but I never really tried implementing it.

IF I get AC on this problem I'll get back to you

jingye
New poster
Posts: 21
Joined: Sat Sep 07, 2002 5:28 am
both methods should run in time.

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:
Did you get AC on this problem?
Or do u know the time limit is now 10 seconds?

So, if these methods I've described can run under 10 secs, then I will redo my implementation, may be I've missed something...

Jo

jingye
New poster
Posts: 21
Joined: Sat Sep 07, 2002 5:28 am
Yes, I have two different programs that get AC under the time limit.

junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore
Just wondering, what's the complexity of your LIS program? I figured my O(n log n) implementation wouldn't work in this problem (it makes use of the fact that if i > j and j > k then i > k, which doesn't hold here)...

Thanks.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
How can I obtain a time-complexity of LIS O(nlogn) ??
I think, that it is an algorithm, which have O(n^2) time-complexity ...

Regards
Dominik

jingye
New poster
Posts: 21
Joined: Sat Sep 07, 2002 5:28 am
my LIS program is O(n^2).

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:
Hi, can u send me your code, so I can see what's wrong with my solution?

Or may I send u my code and u see what's wrong for me?

Any help will be good for me!

Thanks!

Jo

junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore
I thought about this problem, and still have no idea how you got a O(n^2) running in time. How do you check if a word is an edit step ladder of another? Mine runs a loop through the string... Is yours any faster?

Still thinking...

jingye
New poster
Posts: 21
Joined: Sat Sep 07, 2002 5:28 am
Sorry, I misunderstood you. No, my program is the same as yours. It should run in time.