fork download
  1. #use strict;
  2. use warnings;
  3. local $SIG{__WARN__} = sub {$w = shift};
  4. use Time::HiRes;
  5. $|++;
  6.  
  7. my @lines = <>;
  8. my $n = 50000;
  9. my $long = '';
  10. $long .= qw{( )}[ int(rand() * 2) ] for 1 .. $n;
  11.  
  12. push @lines, $long;
  13.  
  14. my @lines2 = @lines;
  15.  
  16. my $start = Time::HiRes::time();
  17.  
  18. my $depth = 40;
  19. my $pre_regex2 = '(?:\((?:\d+,)*(?:\)|' x $depth . '(?!)' . '\)))+' x $depth;
  20. # (?:
  21. # \(
  22. # (?:\d+,)* (?:
  23. # \)
  24. # |
  25. # (?!)\)
  26. # )
  27. # )+
  28. my $regex2 = qr/$pre_regex2/;
  29. my $regex3 = qr/(?:\d+,){2,}/;
  30.  
  31. for my $string (@lines2){
  32.  
  33. while (
  34. $string =~ s/$regex2/
  35. $a=$&,
  36. $b=()=$a=~m{[)(]}g,
  37. $a=~s{\d+,}{$b+=$&,''}eg,
  38. "$b,"
  39. /eg
  40. or
  41. $string =~ s/$regex3/
  42. $a=$&,
  43. $b=0,
  44. $a=~s{\d+,}{$b+=$&,''}eg,
  45. "$b,"
  46. /eg
  47. )
  48. {}
  49. my @groups = $string =~ m/\d+/g;
  50. my $max=0;
  51. for my $group (@groups){
  52. $max < $group and $max = $group
  53. }
  54. my $occ = 0;
  55. for my $group (@groups){
  56. $occ += $max == $group
  57. }
  58. $max or $occ=1;
  59. print "$max $occ\n";
  60. }
  61.  
  62. print Time::HiRes::time() - $start, $/;
  63.  
  64. $start = Time::HiRes::time();
  65.  
  66. my $regex =
  67. \( # match an opening parenthesis
  68. (?0)* # recurse to capture full pattern
  69. \) # match a closing parenthesis
  70. }x;
  71.  
  72. for my $string (@lines)
  73. {
  74. my $max = 0;
  75. my $occ = 1;
  76. my @groups = ();
  77. my $pred = -1;
  78. my $i = 0;
  79. while ($string =~ m/$regex/g){
  80. $pred == $-[0] or $i++;
  81. $groups[$i] += $+[0] - $-[0];
  82. $pred = $+[0];
  83. }
  84.  
  85. if (@groups)
  86. {
  87. @groups = sort { $a <=> $b } @groups;
  88. $max = $groups[-1];
  89.  
  90. for my $i (reverse 0 .. $#groups - 1)
  91. {
  92. if ( $groups[$i] == $groups[-1])
  93. {
  94. ++$occ;
  95. }
  96. else
  97. {
  98. last;
  99. }
  100. }
  101. }
  102.  
  103. print "$max $occ\n";
  104. }
  105.  
  106. print Time::HiRes::time() - $start, $/;
  107.  
  108. __DATA__
  109. )((())))(()())
  110. ))(
  111. ()(())()
  112. ((((()(((
  113. (()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())()(((()())()(()((())()(())(()))
  114.  
Success #stdin #stdout #stderr 3.5s 10424KB
stdin
Standard input is empty
stdout
10446 1
0.467826843261719
10446 1
3.02168297767639
stderr
Name "main::w" used only once: possible typo at prog.pl line 3.