Guest User

Untitled

a guest
Mar 26th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2.     class Node {
  3.         public $leaf;
  4.         public $kids = array();
  5.  
  6.         function __construct($leaf) {
  7.             $this->leaf = $leaf;
  8.         }
  9.  
  10.         function childExists($childVal) {
  11.             return (array_key_exists($childVal, $this->kids));
  12.         }
  13.  
  14.         function addChild($childVal, $isLeaf) {
  15.             $newNode = new Node($isLeaf);
  16.             $this->kids[$childVal] = $newNode;
  17.             return $this->kids[$childVal];
  18.         }
  19.  
  20.         function getChild($childVal) {
  21.             return $this->kids[$childVal];
  22.         }
  23.  
  24.         function hasChilds() {
  25.             return (!empty($this->kids));
  26.         }
  27.  
  28.         function checkLeaf() {
  29.             return $this->leaf;
  30.         }
  31.     }
  32.  
  33.     fscanf(STDIN, "%d", $numOfTestCases);
  34.  
  35.     for ($i = 0; $i < $numOfTestCases; $i++) { //Loop for reading test cases
  36.         fscanf(STDIN, "%d", $numOfPhoneNumbers);
  37.  
  38.         $isConsistent = true;
  39.         $startNode = new Node(false);
  40.  
  41.         for ($j = 0; $j < $numOfPhoneNumbers; $j++) { //Loop for reading phone numbers of each test case
  42.             fscanf(STDIN, "%d", $newNumber);
  43.  
  44.             if ($isConsistent) { // Does not need to check new number if list is already inconsistent
  45.                 $numArr = str_split(strval($newNumber)); // Spitting new number into array
  46.                 $currNode = $startNode;
  47.                 $isConsistent = true;
  48.  
  49.                 foreach ($numArr as $k => $numstr) { // Loop for checking each digit
  50.                     if ($currNode->checkLeaf()) { // If current node is a leaf
  51.                         $isConsistent = false;
  52.                         break;
  53.                     } else {
  54.                         if (!$currNode->childExists($numstr)) {
  55.                             if ($k == count($numArr)-1) {
  56.                                 $currNode = $currNode->addChild($numstr, true);
  57.                             } else {
  58.                                 $currNode = $currNode->addChild($numstr, false);
  59.                             }
  60.                         } else {
  61.                             $currNode = $currNode->getChild($numstr);
  62.                         }
  63.                     }
  64.                 }
  65.  
  66.                 if ($currNode->hasChilds()) $isConsistent = false;
  67.             }  
  68.         }
  69.  
  70.         $newAnswer = ($isConsistent) ? "YES" : "NO";
  71.         $ansString = ($i == 0) ? $newAnswer : $ansString."\n".$newAnswer;
  72.     }
  73.  
  74.     fwrite(STDOUT, $ansString);
  75.  
  76.     exit(0);
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment