189. Perl-like Substr
time limit per test: 0.25
memory limit per test: 4096
input: standard input
output: standard output
A small Russian offshore company H&H decided to strike the world community and develop their own analogue (faster and with lots of features) of a well-known scripting language Perl. You are hired to participate in this project and your task is very simple - to code the function substr.
Function substr writes and reads separate substrings:
$value = substr($string, $offset, $count);
$value = substr($string, $offset);
substr($string, $offset, $count) = $newstring;
substr($string, $offset) = $newstring;
$string (symbol $ in Perl marks a scalar variable, the strings in Perl are scalar variables) contains the initial string. $offset defines the beginning of the substring. Positive values of $offset are counted out from the beginning of the string, negative - from the end. If the $offset equals to zero, the substring begins from the first symbol of string. The third argument $count defines the length of the substring. If $count is omitted, substr returns everything to the end of the string. If $count is negative, substr leaves that many characters off the end of the string.
Function substr can be used not only to get the substring from the string, but also to replace the substring with the new string. In the last case the lengths of the old substring and the new one can be different.
You are given a simple Perl-like program (refer to the input and output specifications) and your task is to output the result of its execution.
The first line contains two natural numbers N (1<=N<=20) - the number of initialization lines in the program and M (1<=M<=300) - the number of remaining lines in the program. Next N lines (no one more than 10000 symbols) contain the string initialization in the following form:
$name = "value";
$name is the name of the string variable (always starts with symbol $). The name of the variable consists of up to 20 (including symbol $) alfa-numerical letters (and the $ in the beginning). The name of the variable is followed by symbol "=", and then follows the value of the variable - a sequence of alfa-numerical symbols and the punctuators (space, comma, dot, hyphen, underline, colon, exclamation and question marks) enclosed in double quotes. The length of the value is not more than 255 symbols. The name of the variable, symbol "=", and the value can be delimited by one or more spaces. There is always a semicolon at the end of the line.
Each of the following M lines contains the line of the program in one of the following 6 types:
1. print($name); Output the value of variable $name
2. print(substr($name, $o[, $c])); Output the substring from variable $name
3. $name1 = $name2; Assign the value of variable $name1 to the value of variable $name2
4. $name1 = substr($name2, $o[, $c]); Assign the value of variable $name1 to the substring from variable $name2
5. substr($name1, $o[, $c]) = $name2; Replace the substring in $name1with the value of $name2
6. substr($name1, $o[, $c]) = substr($name2, $o[, $c]); Replace the substring in $name1 with the substring from $name2
The square brackets indicate that the third argument in substr can be omitted. There may be no spaces inside the names of the variables and the names of functions, but there may be one or several spaces in all other places. There is symbol ";" at the end of each line, the length of each line is not more than 255 symbols. The values of the variables which weren't initialized in the first N lines of the input file are considered as empty strings "". The total number of different variables in the program is not more than 100. No variable value will have the length of more than 1000 symbols in the course of the program execution.
The parameters $c and $o have been chosen so, that the substr returns the "correct" substring, i.e. the beginning and the end of the substring are within the initial string, and the beginning proceeds the end (substring is not empty).
You must assume that names of variables are case sensitive.
The output file must contain the result of the program execution - one line for each print() operator. No additional symbols, spaces or line brakes in the output file are permitted.
$a = "0123456789";
$b = "abcdefghigklmn";
print( substr($b, 1, 2) );
substr($b, 0, 1) = substr($a, 2, 7);
$c = $b;
print(substr($a, -2, -1));
print(substr($a, -6, 2));
print(substr($a, 1, 2));
|Author:||Ilya V. Elterman
|Resource:||ACM International Collegiate Programming Contest 2003-2004
North-Eastern European Region, Southern Subregion
|Date:||2003 October, 9
|Server time: 2017-09-20 16:53:26||Online Contester Team © 2002 - 2016. All rights reserved.|