Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello monks, Someone sent me a link to www.interviewstreet.com and i tried one of those challenges called: Find strings for fun. I have submitted a working code but after 3 tests it fails with "Out of Memory" I have tried a few attempts without real improvement, it gets a bit faster but still fails. Maybe you can have a look at it and let me know which parts can be improved. TIA
Inputs: 2 aab aac 3 3 8 24 Output: aab c INVALID #!/usr/bin/perl use strict; use warnings; $| = 1; # flush output # Get inputs my $n = 0; # number of strings chomp( $n = <STDIN> ); my $i = 0; # line counter my $w = ''; # input string my @S = (); # uniq strings from inputs while ( $i++ < $n ) { chomp ( $w = <STDIN> ); push @S, get_uniq_strings( $w ); } # Get the queries my $q = 0; chomp( $q = <STDIN> ); $i = 0; my $k = ''; my @queries = (); while ( $i++ < $q ) { chomp( $k = <STDIN> ); push @queries, $k - 1; } # Output @S = make_uniq( \@S ); my $uniq_len = scalar @S; foreach ( @queries ) { print (( $_ <= $uniq_len ) ? "$S[ $_ ]\n" : "INVALID\n" ); } exit; sub get_uniq_strings { my $string = shift; my @a = (); my $len = length $string; for ( my $i = 1; $i <= $len; $i++ ) { for ( my $offset = 0; $offset < $len - $i + 1; $offset++ ) { my $sub_str = substr $string, $offset, $i; push @a, $sub_str if $sub_str; } } @a; } sub make_uniq { my $str_array = shift; my $uniq_array = {}; foreach( @{ $str_array } ) { $uniq_array->{ $_ }++ if $_; } sort keys %{ $uniq_array }; }

In reply to finding strings by duyet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (3)
    As of 2014-09-20 00:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (151 votes), past polls