more useful options | |
PerlMonks |
Re^5: How to match more than 32766 times in regex?by rsFalse (Chaplain) |
on Dec 01, 2015 at 22:26 UTC ( [id://1149084]=note: print w/replies, xml ) | Need Help?? |
Ah yes, in this node Re^2: Complex regular subexpression recursion limit I didn't get an answer :/ . Today I was solving another problem (and encountered same limitation). Full problem was: given a string (up to 1e5 length) consisting of '0' and '1', answer what is the length of the longest alternating subsequence if you are able to choose and invert one substring. For example, given a string '100111', I can invert substring from 3rd to 4th character ( substr $line, 2, 2, (substr $line, 2, 2) =~ y/01/10/r ), and then string become '101011' and has alternating subsequence (indexes: 0,1,2,3,4 or 0,1,2,3,5). I wanted to solve that problem with regexes (I knew that I can solve it other way), so I tried to count /1+/ and /0+/ (this is the answer of longest alternating subsequence if no inversions are made). I thought that I can do: , but I decided to stay with zeroes and ones, and wrote () = $line =~ /(.)\1*/g (as I shown). Later I add to $len: /(.)\1\1|(.)\2.*(.)\3/ + /(.)\1/, because each regex if succedes it gives +1 to the possible length of subsequence after one inversion. I often try to solve problems from competitive programming online sites or sites like projecteuler.net and I practise do it with Perl. After I used to calc all the sum: - it consumed too much time when solving input line '01' x 5e4; upd: was bad example with reversion, now fixed to inversion.
In Section
Seekers of Perl Wisdom
|
|