diff --git a/tsa_php/src/ApartmentAllocation.php b/tsa_php/src/ApartmentAllocation.php index 7d95f4a..5575b68 100644 --- a/tsa_php/src/ApartmentAllocation.php +++ b/tsa_php/src/ApartmentAllocation.php @@ -1,16 +1,60 @@ \ No newline at end of file diff --git a/tsa_php/src/PalindromeQueries.php b/tsa_php/src/PalindromeQueries.php index 38d4a6a..54225eb 100644 --- a/tsa_php/src/PalindromeQueries.php +++ b/tsa_php/src/PalindromeQueries.php @@ -22,7 +22,12 @@ public function __construct($n) { * @param int $value The value to update at the specified index. */ public function update($index, $value) { - // YOUR CODE HERE + $index += $this->n; + $this->tree[$index] = $value; + while ($index > 1) { + $index >>= 1; + $this->tree[$index] = ($this->tree[$index * 2] + $this->tree[$index * 2 + 1]) % MOD; + } } /** @@ -32,7 +37,15 @@ public function update($index, $value) { * @return int The sum of values in the range [l, r]. */ public function query($l, $r) { - // YOUR CODE HERE + $l += $this->n; + $r += $this->n + 1; + $result = 0; + while ($l < $r) { + if ($l % 2 == 1) $result = ($result + $this->tree[$l++]) % MOD; + if ($r % 2 == 1) $result = ($result + $this->tree[--$r]) % MOD; + $l >>= 1; + $r >>= 1; + } return $result; } } @@ -43,7 +56,10 @@ public function query($l, $r) { * @return array An array where hashPower[i] = HASH^i % MOD. */ function initializeHashPowers($length) { - // YOUR CODE HERE + $hashPower = [1]; + for ($i = 1; $i <= $length; $i++) { + $hashPower[$i] = ($hashPower[$i - 1] * HASH) % MOD; + } return $hashPower; } @@ -55,7 +71,17 @@ function initializeHashPowers($length) { * @return array An associative array containing the forward and backward hash segment trees. */ function initializeHashTables($n, $s, $hashPower) { - // YOUR CODE HERE + $fwdHash = new HashSegmentTree($n); + $bckHash = new HashSegmentTree($n); + + for ($i = 0; $i < $n; $i++) { + $val = (ord($s[$i]) * $hashPower[$i]) % MOD; + $fwdHash->update($i, $val); + + $revVal = (ord($s[$i]) * $hashPower[$n - 1 - $i]) % MOD; + $bckHash->update($n - 1 - $i, $revVal); + } + return ['fwdHash' => $fwdHash, 'bckHash' => $bckHash]; } @@ -67,9 +93,59 @@ function initializeHashTables($n, $s, $hashPower) { * @return array An array of results for palindrome queries ("YES" or "NO"). */ function processOperations($n, $operations, $s) { - // YOUR CODE HERE + $hashPower = initializeHashPowers($n); + $trees = initializeHashTables($n, $s, $hashPower); + $fwd = $trees['fwdHash']; + $bck = $trees['bckHash']; + $sArr = str_split($s); + $results = []; + + foreach ($operations as $op) { + if ($op[0] == 1) { + // Update operation + $k = $op[1] - 1; + $x = $op[2]; + $sArr[$k] = $x; + $fwd->update($k, (ord($x) * $hashPower[$k]) % MOD); + $bck->update($n - 1 - $k, (ord($x) * $hashPower[$n - 1 - $k]) % MOD); + } else if ($op[0] == 2) { + // Query operation + $a = $op[1] - 1; + $b = $op[2] - 1; + $fwdHash = $fwd->query($a, $b); + $bckHash = $bck->query($n - 1 - $b, $n - 1 - $a); + + if ($a <= $b) { + $leftShift = $n - 1 - $b - $a; + $fwdHash = ($fwdHash * $hashPower[$leftShift]) % MOD; + } + + $results[] = ($fwdHash == $bckHash) ? "YES" : "NO"; + } + } + return $results; } +// Read input +fscanf(STDIN, "%d %d", $n, $m); +$s = trim(fgets(STDIN)); +// Read m operations +$operations = []; +for ($i = 0; $i < $m; $i++) { + $line = explode(' ', trim(fgets(STDIN))); + $type = (int)$line[0]; + if ($type == 1) { + $operations[] = [1, (int)$line[1], $line[2]]; + } else { + $operations[] = [2, (int)$line[1], (int)$line[2]]; + } +} + +// Process and print results +$results = processOperations($n, $operations, $s); +foreach ($results as $res) { + echo $res . "\n"; +} ?> \ No newline at end of file diff --git a/tsa_php/src/TowerOfHanoi.php b/tsa_php/src/TowerOfHanoi.php index d937dac..ad9ad95 100644 --- a/tsa_php/src/TowerOfHanoi.php +++ b/tsa_php/src/TowerOfHanoi.php @@ -13,6 +13,14 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxiliaryStack) { // YOUR CODE HERE + if ($diskNumber == 1) { + $moves[] = [$sourceStack, $destinationStack]; + return; + } + + moveDisk($diskNumber - 1, $moves, $sourceStack, $auxiliaryStack, $destinationStack); + $moves[] = [$sourceStack, $destinationStack]; + moveDisk($diskNumber - 1, $moves, $auxiliaryStack, $destinationStack, $sourceStack); } /** @@ -23,6 +31,13 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxili function towerOfHanoi($numberOfDisks) { // YOUR CODE HERE + $moves = []; + moveDisk($numberOfDisks, $moves, 1, 3, 2); + + echo count($moves) . "\n"; + foreach ($moves as [$from, $to]) { + echo "$from $to\n"; + } } ?>