Use Before SET
PHP
Hard
3 views
Problem Description
Commands: SET name value, PRINT name. Count how many PRINT happen before a name is ever SET.
Input Format
First line q. Next q lines.
Output Format
One integer count.
Sample Test Case
Input:
6
PRINT a
SET a 1
PRINT a
PRINT b
SET b 2
PRINT b
Official Solution
<?php
$inputLines=preg_split('/\\R/', trim(stream_get_contents(STDIN)));
if(!$inputLines || trim($inputLines[0])==='') exit;
$q=intval($inputLines[0]);
$seen=[]; $bad=0;
for($i=1;$i<=$q;$i++){
$tokens=preg_split('/\\s+/', trim($inputLines[$i] ?? ''), 3);
if(!$tokens[0]) continue;
if($tokens[0]==='SET') $seen[$tokens[1] ?? '']=true;
else{
$name=$tokens[1] ?? '';
if(!array_key_exists($name,$seen)) $bad++;
}
}
echo $bad;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!