Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*********************************
- -- BRUTE FORCE STRING MATCHING
- *********************************/
- function brute_force($pattern, $subject)
- {
- $n = strlen($subject);
- $m = strlen($pattern);
- for ($i = 0; i < $n-$m; $i++) {
- $j = 0;
- while ($j < $m && $subject[$i+$j] == $pattern[$j]) {
- $j++;
- }
- if ($j == $m) return $i;
- }
- return -1;
- }
- echo brute_force('o wo', 'hello world!');
- /*********************************
- -- BOYER-MOORE STRING SEARCHING
- *********************************/
- /**
- * Pattern we're searching for
- *
- * @var string
- */
- $pattern = 'gloria';
- /**
- * The text we're searching in
- *
- * @var string
- */
- $text = 'Sic transit gloria mundi, non transit gloria Gundi!';
- /**
- * Calculates the suffixes for a given pattern
- *
- * @param string $pattern
- * @param array $suffixes
- */
- function suffixes($pattern, &$suffixes)
- {
- $m = strlen($pattern);
- $suffixes[$m - 1] = $m;
- $g = $m - 1;
- for ($i = $m - 2; $i >= 0; --$i) {
- if ($i > $g && $suffixes[$i + $m - 1 - $f] < $i - $g) {
- $suffixes[$i] = $suffixes[$i + $m - 1 - $f];
- } else {
- if ($i < $g) {
- $g = $i;
- }
- $f = $i;
- while ($g >= 0 && $pattern[$g] == $pattern[$g + $m - 1 - $f]) {
- $g--;
- }
- $suffixes[$i] = $f - $g;
- }
- }
- }
- /**
- * Fills in the array of bad characters.
- *
- * @param string $pattern
- * @param array $badChars
- */
- function badCharacters($pattern, &$badChars)
- {
- $m = strlen($pattern);
- for ($i = 0; $i < $m - 1; ++$i) {
- $badChars[$pattern{$i}] = $m - $i - 1;
- }
- }
- /**
- * Fills in the array of good suffixes
- *
- * @param string $pattern
- * @param array $goodSuffixes
- */
- function goodSuffixes($pattern, &$goodSuffixes)
- {
- $m = strlen($pattern);
- $suff = array();
- suffixes($pattern, $suff);
- for ($i = 0; $i < $m; $i++) {
- $goodSuffixes[$i] = $m;
- }
- for ($i = $m - 1; $i >= 0; $i--) {
- if ($suff[$i] == $i + 1) {
- for ($j = 0; $j < $m - $i - 1; $j++) {
- if ($goodSuffixes[$j] == $m) {
- $goodSuffixes[$j] = $m - $i - 1;
- }
- }
- }
- }
- for ($i = 0; $i < $m - 2; $i++) {
- $goodSuffixes[$m - 1 - $suff[$i]] = $m - $i - 1;
- }
- }
- /**
- * Performs a search of the pattern into a given text
- *
- * @param string $pattern
- * @param string $text
- */
- function boyer_moore($pattern, $text)
- {
- $n = strlen($text);
- $m = strlen($pattern);
- $goodSuffixes = array();
- $badCharacters = array();
- goodSuffixes($pattern, &$goodSuffixes);
- badCharacters($pattern, &$badCharacters);
- $j = 0;
- while ($j < $n - $m) {
- for ($i = $m - 1; $i >= 0 && $pattern[$i] == $text[$i + $j]; $i--);
- if ($i < 0) {
- // note that if the substring occurs more
- // than once into the text, the algorithm will
- // print out each position of the substring
- echo $j;
- $j += $goodSuffixes[0];
- } else {
- $j += max($goodSuffixes[$i], $badCharacters[$text[$i + $j]] - $m + $i + 1);
- }
- }
- }
- // search using Boyer-Moore
- // will return 12 and 38
- boyer_moore($pattern, $text);
- /*********************************
- -- RABIN-KARP STRING SEARCHING
- *********************************/
- function hash_string($str, $len)
- {
- $hash = '';
- $hash_table = array(
- 'h' => 1,
- 'e' => 2,
- 'l' => 3,
- 'o' => 4,
- 'w' => 5,
- 'r' => 6,
- 'd' => 7,
- );
- for ($i = 0; $i < $len; $i++) {
- $hash .= $hash_table[$str{$i}];
- }
- return (int)$hash;
- }
- function rabin_karp($text, $pattern)
- {
- $n = strlen($text);
- $m = strlen($pattern);
- $text_hash = hash_string(substr($text, 0, $m), $m);
- $pattern_hash = hash_string($pattern, $m);
- for ($i = 0; $i < $n-$m+1; $i++) {
- if ($text_hash == $pattern_hash) {
- return $i;
- }
- $text_hash = hash_string(substr($text, $i, $m), $m);
- }
- return -1;
- }
- // 2
- echo rabin_karp('hello world', 'ello');
- /*********************************
- -- MORRIS-PRATT STRING SEARCHING
- *********************************/
- /**
- * Pattern
- *
- * @var string
- */
- $pattern = 'mollis';
- /**
- * Text to search
- *
- * @var string
- */
- $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eleifend nisi viverra ipsum elementum porttitor quis at justo. Aliquam ligula felis, dignissim sit amet lobortis eget, lacinia ac augue. Quisque nec est elit, nec ultricies magna. Ut mi libero, dictum sit amet mollis non, aliquam et augue';
- /**
- * Preprocess the pattern and return the "next" table
- *
- * @param string $pattern
- */
- function preprocessMorrisPratt($pattern, &$nextTable)
- {
- $i = 0;
- $j = $nextTable[0] = -1;
- $len = strlen($pattern);
- while ($i < $len) {
- while ($j > -1 && $pattern[$i] != $pattern[$j]) {
- $j = $nextTable[$j];
- }
- $nextTable[++$i] = ++$j;
- }
- }
- /**
- * Performs a string search with the Morris-Pratt algorithm
- *
- * @param string $text
- * @param string $pattern
- */
- function MorrisPratt($text, $pattern)
- {
- // get the text and pattern lengths
- $n = strlen($text);
- $m = strlen($pattern);
- $nextTable = array();
- // calculate the next table
- preprocessMorrisPratt($pattern, $nextTable);
- $i = $j = 0;
- while ($j < $n) {
- while ($i > -1 && $pattern[$i] != $text[$j]) {
- $i = $nextTable[$i];
- }
- $i++;
- $j++;
- if ($i >= $m) {
- return $j - $i;
- }
- }
- return -1;
- }
- // 275
- echo MorrisPratt($text, $pattern);
- //=== END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement