Store Variables by Name
PHP
Medium
4 views
Problem Description
Commands: SET name value, GET name. Save values by name. GET prints value or NOT_FOUND.
Input Format
First line q. Next q lines.
Output Format
Outputs for GET.
Sample Test Case
Input:
7
SET a 5
SET b 9
GET a
GET c
SET a 2
GET a
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++){
$parts=preg_split('/\\s+/', trim($inputLines[$i] ?? ''), 3);
if(!$parts[0]) continue;
if($parts[0]==='SET') $mp[$parts[1] ?? '']=$parts[2] ?? '';
else{
$k=$parts[1] ?? '';
$output[] = array_key_exists($k,$mp) ? $mp[$k] : 'NOT_FOUND';
}
}
echo implode(PHP_EOL,$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!