Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created March 12, 2013 22:14
Show Gist options
  • Select an option

  • Save h2rd/5147583 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/5147583 to your computer and use it in GitHub Desktop.
Fb Challenge
<?php
# please use php -f input.php < input00.txt > output00.txt
$stdin = fopen('php://stdin', 'r');
$str = trim(fread($stdin, 1000));
fclose($stdin);
$total = strlen($str);
$count = 0;
$end = $start = 0;
$stack = array();
do {
$end++;
$sub = substr($str, $start, $end);
if ($end >= $total) {
$start++;
$end = 0;
}
if (!isset($stack[$sub]) && strstr($str, $sub)) {
$stack[$sub] = true;
$count ++;
}
} while($start < $total);
$stdout = fopen('php://stdout', 'w');
fwrite($stdout, $count);
fclose($stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment