From: Sergey Volkov Date: 2008-01-22T19:20:20+09:00 Subject: Re: [QUIZ] Longest Repeated Substring (#153) ------=_NextPart_000_0A4D_01C85CB6.7BC09100 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 7bit Suffix tree is brilliant method, but has been invented long time ago, besides, others submitted 'suffix tree' solution already. It was challenge! Please take a look at my solution (attached), in my experiments it's faster than Eric's code. BRs Sergey Volkov -------------------------------------------------------------- # Times [vsv@gx Q153]$ ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux] ### Eric's code [vsv@gx Q153]$ time ruby Q153_EricI.rb i=0A= # repeated substring found=0A= ncpos =3D prev_pos=0A= break=0A= end=0A= # store substring=0A= ht[cs]=3Di=0A= end=0A= break unless ncpos=0A= cpos=3Dncpos=0A= end=0A= rs=0A= end=0A= end=0A= =0A= if __FILE__ =3D=3D $0=0A= if ARGV.first =3D=3D '-U'=0A= ARGV.shift=0A= require 'test/unit'=0A= class TC_LRS < Test::Unit::TestCase=0A= def test_lrs=0A= assert_same nil, ''.longest_repeated_substring=0A= =0A= d=3D'1234567890'=0A= dx =3D (1..9).inject(d){|r,i|r+'.'+r[0,i]}+'.'+d=0A= #1234567890.1.12.123.1234.12345.123456.1234567.12345678.123456789.1234567= 890=0A= =0A= assert_same nil, d.longest_repeated_substring=0A= assert_equal d, (d+d).longest_repeated_substring=0A= assert_equal d, (d+'.'+d).longest_repeated_substring=0A= assert_equal d, dx.longest_repeated_substring=0A= =0A= a=3Dd.gsub( /\d/, 'A' )=0A= ax =3D dx.gsub( /\d/, 'A' )=0A= #AAAAAAAAAA.A.AA.AAA.AAAA.AAAAA.AAAAAA.AAAAAAA.AAAAAAAA.AAAAAAAAA.AAAAAAA= AAA=0A= =0A= assert_equal a, (a+a).longest_repeated_substring=0A= assert_equal a, (a+'.'+a).longest_repeated_substring=0A= assert_not_equal a, ax.longest_repeated_substring=0A= end=0A= end=0A= p :TEST=0A= exit Test::Unit::AutoRunner.run=0A= end=0A= =0A= ###=0A= # Main=0A= inp =3D STDIN.read=0A= # for 'human text' processing (-t):=0A= # replace '\n' inside paragraph with ' '=0A= inp.gsub!(/(\S)\n(\w)/m, '\1 \2') if ARGV.grep( /-.*t/ )[0]=0A= =0A= if lrs =3D inp.longest_repeated_substring=0A= # Output string unless '-n' specified (number only)=0A= puts( lrs ) if ARGV.grep( /-.*n/ ).empty?=0A= # Output size=0A= p [lrs.size]=0A= end=0A= end=0A= ------=_NextPart_000_0A4D_01C85CB6.7BC09100--