duyet has asked for the wisdom of the Perl Monks concerning the following question:
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 }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: finding strings
by CountZero (Bishop) on Dec 09, 2012 at 14:06 UTC | |
|
Re: finding strings
by davido (Cardinal) on Dec 09, 2012 at 17:43 UTC | |
by CountZero (Bishop) on Dec 09, 2012 at 18:58 UTC |