ClarkeRubber

UNSW ProgComp: Problem 5 - 2011

Jul 19th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. CARVES
  5. FOSTER
  6. COGENT
  7. SCRIPT
  8. ATTEND
  9. ENRAGE
  10. CALVES
  11. UNREST
  12. CAPPED
  13. LETHAL
  14. EASTER
  15. SOCCER
  16. RENDER
  17. ESCAPE
  18. MAPLES
  19. CRANIA
  20. SCURRY
  21. DECENT
  22. BUGLES
  23. ENLIST
  24. RESETS
  25. VERIFY
  26. BOUGHT
  27. ENDEAR
  28. STYLED
  29. BARBER
  30. RAISES
  31. ESTATE
  32. TAKERS
  33. BASKED
  34. ACCENT
  35. ADVERT
  36. TALKIE
  37. BRUTAL
  38. STARRY
  39. TAUTLY
  40. WEASEL
  41. AGENDA
  42. RAIDED
  43. STRAIT
  44. REELER
  45. ESSAYS
  46. TAPERS
  47. MASSES
  48. TASTED
  49. PRIMAL
  50. BOOTHS
  51. GROTTO
  52. RADIUM
  53. STADIA
  54. BARFLY
  55. INSERT
  56. OCTAVE
  57. SERIAL
  58. SHAVES
  59. PIERCE
  60. ANTLER
  61. SCARES
  62. STOLEN
  63. REPUTE
  64. HARPER
  65. TASTER
  66. ANEMIA
  67. OCTANE
  68. STILLS
  69. POSTER
  70. END;
  71.  
  72. function check_match($array){
  73.     $len = $array['len'];
  74.     for($x = 0; $x < $len; $x++){
  75.         for($y = 0; $y < $len; $y++){
  76.             if($array[$x][$y] != $array[$y][$x]){
  77.                 return false;
  78.             }
  79.         }
  80.     }
  81.     return true;
  82. }
  83.  
  84. function match_word($array, $word, $len){
  85.     //$len = strlen($word);
  86.     $temp_word = str_split($word);
  87.     $out_ar = array();
  88.     foreach($array as $key => $value){
  89.         if(!in_array($word, $value['hist'])){
  90.             for($x = 0; $x < $len; $x++){
  91.                 for($y = 0; $y < $len; $y++){
  92.                     if(!isset($value[$x][$y])){
  93.                         $continue = true;
  94.                         for($x2 = 0; $x2 < $value['len']; $x2++){
  95.                             $letter = $word[$x2];
  96.                             $letter2 = $value[$x2][$y];
  97.                             if($value[$x2][$y] != $temp_word[$x2] && isset($value[$x2][$y])){
  98.                                 //$y = $len;
  99.                                 //echo "Breaking Y: $y\n";
  100.                                 $continue = false;
  101.                                 break;
  102.                             }
  103.                         }
  104.                         if($continue && !in_array($word, $value['hist'])){
  105.                             $temp_ar = $array[$key];
  106.                             $temp_ar['hist'][] = $word;
  107.                             for($x2 = 0; $x2 < $value['len']; $x2++){
  108.                                 //echo "    Inserting $word into {$value['hist'][0]} $x2\n";
  109.                                 $temp_ar[$x2][$y] = $temp_word[$x2];
  110.                                 $temp_ar[$y][$x2] = $temp_word[$x2];
  111.                             }
  112.                             if(check_match($temp_ar)){
  113.                                 $array[$key] = $temp_ar; //I would like to make this line $array[] = $temp_ar, but this uses gigabytes of storage.
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.     }
  121.     return $array;
  122. }
  123.  
  124. function build_array($array){
  125.     foreach($array as $value){
  126.         $temp_str = str_split($value);
  127.         //temp[x][y] = char
  128.         $c = 0;
  129.         foreach($temp_str as $value2){
  130.             $temp[0][$c] = $value2;
  131.             $temp[$c][0] = $value2;
  132.             $c++;
  133.         }
  134.         $temp['hist'][0] = $value;
  135.         $temp['len'] = strlen($value);
  136.         $out[] = $temp;
  137.     }
  138.     return $out;
  139. }
  140.  
  141. function multipleExplode($delimiters = array(), $string = ''){
  142.     if(count($delimiters) == 1){
  143.         return explode($delimiters[0], $string);
  144.     }
  145.     $mainDelim=$delimiters[count($delimiters)-1]; // dernier
  146.     array_pop($delimiters);
  147.     foreach($delimiters as $delimiter){
  148.         $string= str_replace($delimiter, $mainDelim, $string);
  149.     }
  150.     $result = explode($mainDelim, $string);
  151.     return $result;
  152. }
  153.  
  154. $input = multipleExplode(array("\n"), $input);
  155. $word_table = build_array($input);
  156. $len = $word_table[0]['len'];
  157.  
  158. foreach($input as $word){
  159.     //echo "Current Word: $word\n\n";
  160.     $word_table = match_word($word_table, $word, $len);
  161. }
  162.  
  163. $output = array();
  164.  
  165. foreach($word_table as $key => $value){
  166.     //echo "printing...\n";
  167.     $cont = true;
  168.     for($x = 0; $x < $len; $x++){
  169.         for($y = 0; $y < $len; $y++){
  170.             if(!isset($value[$x][$y])){
  171.                 //echo "    $key $x $y IS EMPTY\n\n";
  172.                 $cont = false;
  173.                 break 2;
  174.             }
  175.         }
  176.     }
  177.     if($cont && !in_array($value, $output)){
  178.         $output[] = $value;
  179.     }
  180. }
  181.  
  182. foreach($output as $value){
  183.     for($x = 0; $x < $len; $x++){
  184.         for($y = 0; $y < $len; $y++){
  185.             echo $value[$x][$y];
  186.         }
  187.         echo "\n";
  188.     }
  189.     echo "\n";
  190. }
Advertisement
Add Comment
Please, Sign In to add comment