D. Perse-script

Codeforces
IDCF72D
Time7000ms
Memory256MB
Difficulty
expression parsing
English · Original
Chinese · Translation
Formal · Original
Two good friends were trying to make a new programming language called Perse-script. The most important part of this language is strings. A string in Perse-script is put between characters _"_ So for example _"Hello"_ is a string. But _Hello_ is a variable name or a keyword, which are not considered in this problem. Perse-script is function-based. So there are no operators in this language. For example for summing two numbers you have to write _sum(a,b)_ and not _a+b_. There are several functions for working on strings. These include: * _concat(x,y)_ is a function which gets two strings _x_ and _y_ and puts _y_ at the end of _x_ and returns the result. For example _concat("Hello","World")_ returns _"HelloWorld"_. * _reverse(x)_ gets a single string _x_ and reverses it. For example _reverse("Hello")_ returns _"olleH"_. * _substr(x,a,b)_ gets a string _x_ and two integers _a_ and _b_ (1 ≤ _a_ ≤ _b_ ≤ _n_, where _n_ is the length of _x_). And returns the substring of _x_ between indexes _a_ and _b_, inclusive. For example _substr("Hello",2,4)_ returns _"ell"_. * _substr(x,a,b,c)_ is another version of substr which works just like the last one but _c_ is the step of adding. _c_ is positive. For example _substr("HelloWorld",1,10,2)_ returns _"Hlool"_. This substr means that you put the _a_th character , and then every _c_th character until you reach _b_. You're going to manipulate the string part of Perse-script. Given a string expression, you should print its result. It is guaranteed that the expression contains only strings shown by characters _"_ and the above functions. Commands in Perse-script are case-insensitive. So to call _substr_ function you can write _SUBsTr()_. But you can't print as the result _"hElLo"_ instead of printing _"Hello"_. See the samples for more information. ## Input A single line containing the correct expression. It is guaranteed that the total length of this expression does not exceed 103 and that all the integers used in it are less than or equal to 100 by absolute value. The given string is non-empty. All strings in the input which are placed between _"_s consist of uppercase and lowercase Latin letters only. ## Output Print in single line the resulting string. It is guaranteed that an answer exists and that the length of the answer does not exceed 104. It is guaranteed that the answer is non-empty. [samples]
两位好朋友正在尝试创造一种名为 Perse-script 的新编程语言。 该语言最重要的部分是 #cf_span(class=[tex-font-style-underline], body=[strings])。在 Perse-script 中,字符串用字符 _"_ 包围。 例如,_"Hello"_ 是一个字符串。但 _Hello_ 是变量名或关键字,本题中不考虑这些情况。 Perse-script 是基于函数的,因此该语言中没有运算符。例如,要对两个数求和,你必须写 _sum(a,b)_,而不是 _a+b_。 该语言提供了若干用于操作字符串的函数,包括: 你需要处理 Perse-script 的字符串部分。给定一个字符串表达式,你应输出其结果。题目保证表达式仅包含由 _"_ 表示的字符串和上述函数。 Perse-script 中的命令不区分大小写。因此,调用 _substr_ 函数时,你可以写 _SUBsTr()_。但你不能将结果输出为 _"hElLo"_ 而不是 _"Hello"_。 更多细节请参见样例。 输入为一行包含正确表达式的字符串。题目保证该表达式的总长度不超过 #cf_span[103],且其中使用的所有整数的绝对值均不超过 #cf_span[100]。给定的字符串非空。 输入中所有位于 _"_ 之间的字符串仅由大小写拉丁字母组成。 请在单行中输出结果字符串。题目保证答案存在,且答案长度不超过 #cf_span[104],且答案非空。 ## Input A single line containing the correct expression. It is guaranteed that the total length of this expression does not exceed #cf_span[103] and that all the integers used in it are less than or equal to #cf_span[100] by absolute value. The given string is non-empty.All strings in the input which are placed between _"_s consist of uppercase and lowercase Latin letters only. ## Output Print in single line the resulting string. It is guaranteed that an answer exists and that the length of the answer does not exceed #cf_span[104]. It is guaranteed that the answer is non-empty. [samples]
**Definitions** Let $ \Sigma = \{ \text{a}, \text{b}, \dots, \text{z}, \text{A}, \text{B}, \dots, \text{Z} \} $ be the set of Latin letters. Let $ \mathcal{S} = \Sigma^* $ be the set of all finite strings over $ \Sigma $. Let $ \mathcal{F} = \{ \texttt{concat}, \texttt{substr}, \texttt{upper}, \texttt{lower} \} $ be the set of supported string functions (case-insensitive). Each expression is a well-formed string over the alphabet $ \Sigma \cup \{ \texttt{"} , \texttt{(}, \texttt{)}, \texttt{,}, \texttt{ } \} $, where: - Strings are delimited by `"` and contain only characters from $ \Sigma $. - Function calls have the form $ f(s_1, s_2, \dots, s_n) $, where $ f \in \mathcal{F} $, and each $ s_i $ is either a string literal or a nested function call. - Expressions are fully parenthesized and syntactically valid. **Constraints** 1. Total length of input expression $ \leq 103 $. 2. All integer arguments (e.g., in `substr`) satisfy $ |n| \leq 100 $. 3. All string literals consist only of letters in $ \Sigma $. 4. Output length $ \leq 10^4 $. **Objective** Evaluate the given expression recursively according to the following semantics: - $ \texttt{concat}(s_1, s_2) \mapsto s_1 \cdot s_2 $ (string concatenation) - $ \texttt{substr}(s, \text{start}, \text{len}) \mapsto s[\text{start} : \text{start} + \text{len}] $ (substring from index `start` of length `len`; 0-based indexing) - $ \texttt{upper}(s) \mapsto \text{all characters in } s \text{ converted to uppercase} $ - $ \texttt{lower}(s) \mapsto \text{all characters in } s \text{ converted to lowercase} $ Output the resulting string after full evaluation.
Samples
Input #1
"HelloWorld"
Output #1
"HelloWorld"
Input #2
REVerse(substr("helloworld",1,5))
Output #2
"olleh"
Input #3
conCAT(rEveRSE("olleh"),"world")
Output #3
"helloworld"
Input #4
reversE(concAT(substr("hello",1,2),sUbstr("world",1,5,1)))
Output #4
"dlroweh"
Input #5
suBstr("Noruz",1,4,2)
Output #5
"Nr"
API Response (JSON)
{
  "problem": {
    "name": "D. Perse-script",
    "description": {
      "content": "Two good friends were trying to make a new programming language called Perse-script. The most important part of this language is strings. A string in Perse-script is put between characters _\"_ So fo",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 7000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF72D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Two good friends were trying to make a new programming language called Perse-script.\n\nThe most important part of this language is strings. A string in Perse-script is put between characters _\"_\n\nSo fo...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "两位好朋友正在尝试创造一种名为 Perse-script 的新编程语言。\n\n该语言最重要的部分是 #cf_span(class=[tex-font-style-underline], body=[strings])。在 Perse-script 中,字符串用字符 _\"_ 包围。\n\n例如,_\"Hello\"_ 是一个字符串。但 _Hello_ 是变量名或关键字,本题中不考虑这些情况。\n\nPerse-s...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ \\Sigma = \\{ \\text{a}, \\text{b}, \\dots, \\text{z}, \\text{A}, \\text{B}, \\dots, \\text{Z} \\} $ be the set of Latin letters.  \nLet $ \\mathcal{S} = \\Sigma^* $ be the set of all finite...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments