


Element object:
<codingNinjas course=āInterviewPreparationā type=āIndividualCourseā>
<topic name=āAptitudeā>CourseContents</topic>
</codingNinjas>
Tag mapping:
ācodingNinjasā => 1, ātopicā => 2, ācourseNameā => 3, ātypeā => 4, ātopicNameā => 5
The given element is ācodingNinjasā, and it has a single child element ātopicā. The element ācodingNinjasā has two attributes having tags as ācourseā and ātypeā. The element ātopicā has one attribute with the tag as ānameā. So, the encoded string is:
ā1 3 InterviewPreparation 4 IndividualCourse 0 2 5 Aptitude 0 CourseContents 0 0ā
1. āENDā is always encoded as 0.
2. An āElementā can have zero or multiple āAttributesā.
The function āencodeXMLā has the following parameters:
Pointer to an āElementā object.
Mapping of āTAGā name (string) to an integer value.
Structure of āElementā and āAttributeā object:
Element {
String tag;
List<Attribute> attributes;
List<Element *> child;
String value;
}
Attribute {
String tag;
String value;
}
If āElementā stores a list of child elements, then ā(Element => values)ā stores an empty string.
The first line of input contains an integer āTā denoting the number of test cases.
The first line of each test case contains an integer āNā denoting the count of elements, including nested elements.
The following āNā blocks of lines contain the description of āNā elements. Each block is as follows:
1. Each blockās first line contains three space-separated values, integer āIDā denoting the elementās id, āMā denoting the number of āAttributesā, and a string denoting the elementās ātagā. The āIDā is used later on to identify the child elements.
2. The next āMā lines contain two space-separated integers denoting the ātagā and āvalueā of each āAttributeā.
3. The next line contains either of the following:
a. If the element stores a value, it contains the integer 0 and a string denoting the āvalueā in the element, separated by space.
b. Otherwise, an integer āCā denoting the number of child elements.
The next āN - 1ā lines contain two space-separated integers, āAā and āBā, the element with āidā as āBā is the child of the element with āidā as āAā. The given āElementā object always has āidā equal to ā1ā and has no parent.
The next line of each test case contains an integer āSā denoting the number of ātagsā.
The next āSā lines of each test case contain two space-separated values, a string denoting the ātagā name and an integer mapped to this ātagā.
For each test case, return the encoded string.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
2 <= N <= 1000
The count of āAttributesā of all elements doesnāt exceed 1000.
The string āValueā is non-empty and contains only āa-zā and āA-Zā characters.
Time Limit: 1 sec
To encode an element, we need to encode its 'TAG' and āattributesā. If it doesnāt store a āVALUEā, we need to encode its children in the given order. For encoding the children, we first encode their 'TAG' and āattributesā, then encode their children, and so on. We stop this process when we reach an element that stores a āVALUEā. So, we need to encode the list of child elements recursively.
Create a helper function āencodeHelperā that takes parameters as an āelementā object, 'TAG' to integer mapping, and references to a string. The helper function encodes the passed element and appends it to the āRESULTā. After that, it traverses through the list of child elements and makes recursive calls to itself with the child element as a parameter. In the recursive call, it appends the encoded child element to āRESULTā. The recursion ends when an element that stores a āVALUEā is reached.
The helper function, āencodeHelper(Object āELEMENTā, Map 'TAG_TO_INT', String& āRESULTā)ā:
In the āencodeXMLā function, we call āencodeHelperā with parameters: (object of input āELEMENTā, given 'TAG' to integer mapping, an empty string āRESULTā). The string āRESULTā stores the converted output string.
Maximum Island Size in a Binary Tree
Equal Subtree Sums
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators