This page is still under construction.

Parts of this page are still being built. What you see may change.

A Well-Formed Problem

Time limit1sMemory limit128 MB

Summary
Parse a series of XML documents and decide whether each one satisfies six well-formedness rules, reporting the verdict per document.
Level

Medium6 of 10

Topics
String, Stack, Hash map, Implementation
Solved
No attempts yet

Problem

XML (eXtensible Markup Language) has strict formatting requirements, and an XML parser must report anything that breaks the rules of a well-formed document. A document is well-formed when it satisfies every constraint listed below.

XML documents are built from elements, which contain character data and/or other elements. An element's start-tag may also declare attributes. Consider this document:

<?xml version="1.0"?>
<customer>
  <name>
    <first>John</first>
    <last>Doe</last>
  </name>
  <address>
    <street>
      <number>15</number>
      <direction>West</direction>
      <name>34th</name>
    </street>
    <city>New York</city>
    <state-code>NY</state-code>
    <zip-code format="PLUS4">10001-0001</zip-code>
    <country-code>USA</country-code>
  </address>
  <orders/>
</customer>

The identifiers inside angle brackets are the document's elements. In the zip-code element, format is an attribute. Every element except orders has both a start-tag and an end-tag. orders is an empty element: the trailing /> closes it, so it needs no separate end-tag. The first line is a processing instruction for the parser and is not an element.

A document is well-formed if and only if it obeys all of the following rules:

  1. Exactly one element is not contained within any other element. That element is the root (document) element. Above, customer is the root.
  2. Elements must nest properly. A non-empty element's start-tag must be paired with a matching end-tag.
  3. An end-tag's name must match its start-tag's name. Names are case-sensitive, so an element opened with <address> must be closed with </address>.
  4. No attribute may appear more than once in the same start-tag or empty-element tag.
  5. An element must not contain another element with the same name anywhere inside it. For example, an address element may not contain another address element.
  6. Every named attribute must have a value.

Input

The input contains a series of XML documents. Each document begins with a line containing only the processing instruction <?xml version="1.0"?>. The input ends with a line containing only <?end?> -- a sentinel that marks the end of the input; it is not a real XML processing instruction. As in every XML document, white space between elements and attributes is ignored. You may assume the following:

  • The only processing instruction present is the XML version instruction, and it appears only at the start of each document.
  • Element and attribute names are case-sensitive; <Address> and <address> are different.
  • Element and attribute names use only alphanumeric characters and the dash (-).
  • No XML comments appear in the input.
  • Attribute values are always enclosed in double quotes.

Output

For each XML document, print one line: well-formed if the document is well-formed, or non well-formed otherwise. Print the verdicts in the same order as the documents.

Examples8

  1. Example 1

    Input
    <?xml version="1.0"?>
    <acm-contest-problem>
            <title>A Well-Formed Problem</title>
            <text>XML, eXtensible Markup Language, is poised to become the lingua franca of
    structured data communication for the foreseeable future. [...]</text>
            <input>probleme.in</input>
            <output>probleme.out</output>
    </acm-contest-problem>
    <?xml version="1.0"?>
    <shopping-list>
            <items>
                    <item quantity="1" quantity="1">Gallon of milk</item>
                    <item>Frozen pizza
            </items>
    </Shopping-list>
    <errand-list>
            <errand>Get some cash at the ATM
                    <errand>Pick up dry cleaning</errand>
            </errand>
    </errand-list>
    <?end?>
    
    Expected output
    well-formed
    non well-formed
    
  2. Example 2

    Input
    <?xml version="1.0"?>
    <a></a>
    <?end?>
    
    Expected output
    well-formed
    
  3. Example 3

    Input
    <?xml version="1.0"?>
    <customer>
        <name>
            <first>John</first>
            <last>Doe</last>
        </name>
        <address>
            <street>
                <number>15</number>
                <name>34th</name>
            </street>
            <zip-code format="PLUS4">10001-0001</zip-code>
        </address>
        <orders/>
    </customer>
    <?end?>
    
    Expected output
    well-formed
    
  4. Example 4

    Input
    <?xml version="1.0"?>
    <address>
        <street>15</street>
        <address>Nested</address>
    </address>
    <?end?>
    
    Expected output
    non well-formed
    
  5. Example 5

    Input
    <?xml version="1.0"?>
    <Root>
        <child>text</child>
    </root>
    <?end?>
    
    Expected output
    non well-formed
    
  6. Example 6

    Input
    <?xml version="1.0"?>
    <tag attr="1" attr="2">text</tag>
    <?end?>
    
    Expected output
    non well-formed
    
  7. Example 7

    Input
    <?xml version="1.0"?>
    <tag attr>text</tag>
    <?end?>
    
    Expected output
    non well-formed
    
  8. Example 8

    Input
    <?xml version="1.0"?>
    <a></a>
    <b></b>
    <?end?>
    
    Expected output
    non well-formed