fork(3) download
  1. #!/usr/bin/perl
  2. use 5.020;
  3. use warnings;
  4. use feature qw(signatures);
  5. no warnings qw(experimental);
  6.  
  7. sub f($s) {
  8. my ($c1, @s) = split //, $s;
  9. my $c2;
  10. my $n = 1;
  11.  
  12. foreach(@s){
  13. when($c1){
  14. $n++;
  15. }
  16. when($c2){
  17. # nothing.
  18. }
  19. default {
  20. defined $c2 and return 0;
  21. $c2 = $_;
  22. { gp => 1, cg => 1, pc => 1 }->{$c1 . $c2} or next;
  23. ($c1, $c2) = ($c2, $c1);
  24. $n = 1;
  25. }
  26. }
  27.  
  28. return (defined $c2 ? $n : 0);
  29. }
  30.  
  31. foreach(
  32. "gpgpgppppg",
  33. "ggggggggggggggg",
  34. "ccpcpppcccpppcppcpcc",
  35. "ggcgcgcggggcpgcggcgcggggcgcgcc",
  36. ){
  37. say f($_);
  38. }
  39.  
Success #stdin #stdout 0s 6176KB
stdin
Standard input is empty
stdout
6
0
10
0