#P9800. [NERC 2018] JS Minification
[NERC 2018] JS Minification
Background
Translated from Problem J of NERC 2018.
Problem Description
You are given a program, where each line contains or more tokens that can be separated by spaces. You need to “minify” it in the following way.
-
In each line, if there is a part starting with
#, it means this is a comment, and it together with everything after it on the same line will not be executed. -
Convert the source code into a sequence of identifiers by parsing each line from left to right, repeatedly skipping spaces and, from the current parsing position, finding the longest possible identifier. All possible identifiers are listed below:
- Reserved identifiers: any operators, separators, literals, keywords, or library function names that should be kept during minification. A reserved token is a fixed string of non-space ASCII characters that does not contain
#.- Numeric identifiers: a string consisting of a sequence of digits.
- Word identifiers: a string consisting of a sequence of characters from the following set: lowercase letters, uppercase letters, digits,
_,$, and it does not start with a digit.
Note that during minification, the longest character sequence that satisfies the definition of a numeric identifier or a word identifier, but appears in the reserved token list, is treated as a reserved identifier.
During minification, rename words systematically using the following algorithm.
-
Define as the sequence of strings consisting of lowercase letters, sorted by length as the first key and lexicographical order as the second key.
-
Rename the first word encountered in the identifier sequence to the first word in the target word list, and rename all identical words in the identifier sequence to that first word. Then rename the second new word encountered in the identifier sequence to the second word in the target word list, and so on.
In addition, you may delete some unnecessary spaces and newline characters. However, note that after deletion, you must not make some strings that were not identifiers become identifiers, or make some strings that were identifiers become not identifiers.
Input Format
The first line contains an integer , representing the number of identifiers.
The second line contains a space-separated list of reserved identifiers. There are no duplicates in this list, and each has length at least and at most .
The third line contains a single integer , representing the number of lines in the input code.
The next lines each contain one line of code (possibly with leading spaces).
Output Format
Output one line: the result of minifying the input code. Output the parsed identifier sequence with the corresponding renamed identifiers, and it should contain as few spaces as possible. If there are multiple ways, output the one with the fewest spaces and the shortest length.
16
fun while return var { } ( ) , ; > = + ++ - --
9
fun fib(num) { # compute fibs
var return_value = 1, prev = 0, temp;
while (num > 0) {
temp = return_value; return_value = return_value + prev;
prev = temp;
num--;
}
return return_value;
}
fun a(b){var c=1,d=0,e;while(b>0){e=c;c=c+d;d=e;b--;}return c;}
10
( ) + ++ : -> >> >>: b c)
2
($val1++ + +4 kb) >> :out
b-> + 10 >>: t # using >>:
(a+++ +4c )>> :d b->+10>>:e
Hint
Constraints: , .
Translated by ChatGPT 5