Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: the case where regex seems to work slower

by tybalt89 (Monsignor)
on Jul 26, 2017 at 23:03 UTC ( [id://1196124]=note: print w/replies, xml ) Need Help??


in reply to the case where regex seems to work slower

Just fudge things a little :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1196089 use strict; use warnings; $\ = $/; my $dict = 'b'; $_ = '*' . 'a' x (9e4 - 1e4) . "\n"; my($head, $tail) = map length( $_ // '' ), split /\*/, $_; s/\?/[$dict]/g; s/\*/ [^$dict ]* /; my $qr = qr/^$_$/; substr($_, $head, 0, ' '), substr($_, -$tail, 0, ' '), print $_ =~ $qr ? "YES" : "NO" for 'a' x (0 + 1e4) . 'a' x (9e4 - 1e4) . "\n";

This avoids the massive backtrack this particular problem encounters.

Replies are listed 'Best First'.
Re^2: the case where regex seems to work slower
by rsFalse (Chaplain) on Jul 27, 2017 at 10:53 UTC
    I think this do not work at cases when '*' is absent or not in the middle!

      Hey, it worked for the test case you gave :)

      (grumble, grumble, inadequate test cases :( Even these test cases are inadequate, can you figure out why? )

      #!/usr/bin/perl # http://perlmonks.org/?node_id=1196089 use strict; use warnings; $\ = $/; while(<DATA>) { chomp; my $dict = $_; $_ = <DATA>; my($head, $tail) = map length( $_ // '' ), split /\*/; s/\?/[$dict]/g; s/\*/ [^$dict ]* /; my $qr = qr/^$_/; $_ = <DATA>, $head <= length($_) && substr($_, $head, 0, ' '), $tail && $tail <= length($_) && substr($_, -$tail, 0, ' '), print $_ =~ $qr ? "YES" : "NO" for 1 .. <DATA>; } __DATA__ ab a?a 3 aaa aab aaac ab *a?a 3 aaa aab caaa ab a?a* 3 aaa aab aaac b aaaaaaaaaaaaaaaaaaaaaaa*aaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b aaaaaaaaaaaaaaaaaaaaaaa*aaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa* 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa* 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        Sorry, I didn't understand your second sentence, because parentheses were not balanced ;)

        Thank you for improvement to cope with other cases. I've submitted, and it took only 171 ms at worst TC, this is #93 - http://codeforces.com/contest/832/submission/28930515

        Upd.: In this solution there single spaces in regex are used. They were mysterious for me in previous code, and should be taken by negate character class.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1196124]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found