UNSET Tracker
PHP
Hard
4 views
Problem Description
Commands: SET name val, UNSET name, GET name. For GET print value or UNSET.
Input Format
First line q. Next q lines.
Output Format
Outputs for GET.
Sample Test Case
Input:
7
SET a 5
GET a
UNSET a
GET a
SET a 9
UNSET b
GET b
Official Solution
<?php
$inputLines=preg_split('/\\R/', trim(stream_get_contents(STDIN)));
if(!$inputLines || trim($inputLines[0])==='') exit;
$q=intval($inputLines[0]);
$mp=[]; $output=[];
for($i=1;$i<=$q;$i++){
$tokens=preg_split('/\\s+/', trim($inputLines[$i] ?? ''), 3);
if(!$tokens[0]) continue;
if($tokens[0]==='SET') $mp[$tokens[1] ?? '']=$tokens[2] ?? '';
elseif($tokens[0]==='UNSET') unset($mp[$tokens[1] ?? '']);
else{
$k=$tokens[1] ?? '';
$output[] = array_key_exists($k,$mp) ? $mp[$k] : 'UNSET';
}
}
echo implode(PHP_EOL,$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!