← 返回 google 的题目列表Recursive Variable Substitution with Cycle Detection
类型:online_judge
Given a key-value map kv and a template string template, variable references use the form %KEY%, where KEY contains only uppercase letters, digits, and underscores.
Expand every variable reference in the template:
Replace %KEY% with kv[KEY].
Values in kv may themselves contain variable references and must be expanded recursively until no expandable references remain.
If a referenced key does not exist, print ERROR.
If the dependency graph contains a cycle, print CYCLE.
Every % is guaranteed to belong to a valid %KEY% placeholder. You do not need to handle escaped percent signs or unmatched % characters.
Example:
kv = {
USER = admin,
HOME = /%USER%/home
}
template = I am %USER%. My home is %HOME%.
Output:
I am admin. My home is /admin/home.
Input Format
n
KEY_1=VALUE_1
KEY_2=VALUE_2
...
KEY_n=VALUE_n
template
The first line contains the number of entries n.
Each of the next n lines contains one mapping. The text before the first = is the key; everything after it is the value. Values may contain spaces and %KEY% references.
The last line is the template to expand.
Output Format
Print the fully expanded template on success.
Print ERROR if an undefined variable is referenced.
Print CYCLE if there is a cyclic dependency.
Constraints
1 <= n <= 10^5
The total number of characters across all keys, values, and the template is at most 10^6.
Each key appears at most once.
Example
Input
2
USER=admin
HOME=/%USER%/home
I am %USER%. My home is %HOME%.
Output
I am admin. My home is /admin/home.