HDU-4782-Beautiful soup (simulation)

Source: Internet
Author: User
Tags closing tag
Problem description coach Pang has a lot of hobbies. one of them is playing with "tag soup" with the help of beautiful Soup. coach Pang is satisfied with beautiful Soup in every respect, cannot the prettify () method, which attempts to turn a soup into a nicely formatted string. he decides to rewrite the method to pret0000a HTML document according to his personal preference. but Coach Pang is always very busy, so he gives this task to you. considering that you do not know anything about "tag soup" or beautiful Soup, Coach Pang kindly left some information with you:
In web development, "tag soup" refers to formatted markup written for a Web page that is very much like HTML but does not consist of correct HTML syntax and document structure. in short, "tag soup" refers to messy HTML code.
Beautiful soup is a library for parsing HTML tags ents (including "tag soup"). It parses "tag soup" into regular HTML tags ENTs, and creates parse trees for the parsed pages.
The parsed HTML documents obey the rules below.

Html
HTML stands for hypertext markup language.
HTML is a markup language.
A markup language is a set of markup tags.
The tags describe document content.
HTML documents consist of tags and texts.

Tags
HTML is using tags for its syntax.
A tag is composed with special characters: '<', '>' and '/'.
Tags usually come in pairs, the opening tag and the closing tag.
The opening tag starts with "<" and the tagname. It usually ends with a "> ".
The closing tag starts with "</" and the same tagname as the corresponding opening tag. It ends with a "> ".
There will not be any other angle brackets in the specified ENTs.
Tagnames are strings containing only lowercase letters.
Tags will contain no line break ('\ n ').
Identifier t tags, anything occured in the document is considered as text content.

Elements
An element is everything from an opening tag to the matching closing tag (including the two tags ).
The element content is everything between the opening and the closing tag.
Some elements may have no content. They're called empty elements, like <HR> Empty elements can be closed in the opening tag, ending with a "/>" instead of "> ".
All elements are closed either with a closing tag or in the opening tag.
Elements can have attributes.
Elements can be nested (can contain other elements ).
The <HTML> element is the container for all other elements, it will not have any attributes.

Attributes
Attributes provide additional information about an element.
Attributes are always specified in the opening tag after the tagname.
Tag Name and attributes are separated by single space.
An element may have several attributes.
Attributes come in name = "value" pairs like class = "ICPC ".
There will not be any space around the '= '.
All attribute names are in lowercase.

A simple example<A href = "http://icpc.baylor.edu/"> ACM-ICPC </a>
The <A> element defines an HTML link with the <A> tag.
The link address is specified in the href attribute.
The content of the element is the text "ACM-ICPC"
  
You are feeling dimo-after reading all these, when Coach Pang shows up again. He starts to spout for hours about his personal preference and you catch his main points with difficulty. Coach Pang says:

Your task is to write a program that will turn parsed HTML into ents into formatted parse trees. you shoshould print each tag or text content on its own line preceded by a number of spaces that indicate its depth in the parse tree. the depth of the root of the parse tree (the <HTML> tag) is 0. he is satisfied with the tags, so you shouldn't change anything of any tag. for text content, throw away unnecessary white spaces including space (ASCII code 32), tab (ASCII Code 9) and newline (ASCII code 10 ), so that words (sequence of characters without white spaces) are separated by single space. there shoshould not be any trailing space after each line nor any blank line in the output. the line contains only white spaces is also considered as blank line. you quickly realize that your only job is to deal with the white spaces.
Input the first line of the input is an integer t representing the number of test cases.
Each test case is a valid HTML document starts with a <HTML> tag and ends with a The size of the input file will not exceed 20kb.
Output for each test case, first output a line "Case # X:", where X is the case number (starting from 1 ).
Then you shoshould write to the output the formatted parse trees as described above. See sample below for clarification of the output format.
Sample Input
2

Sample output
[pre]Case #1:HintPlease be careful of the number of leading spaces of each line in above sample output. 

Source2013 Asia Chengdu Regional Contest
Train of Thought: pay attention to the following points: ① \ n \ t should be treated as spaces. ② </> ③ Space at the end of the line. ④ <The content cannot be moved>
# include # include char s [50000]; int NXT [50000]; void PS (int x) // output the leading space {puts (""); For (INT I = 0; I scanf ("% d", & T); Len = 0; while (gets (S + Len )! = NULL) // read all {If (! S [0]) continue; Len = strlen (s); s [Len] = ''; // Add a space at the end of Len ++;} In = 0; for (I = 0; I {If (suojin) pS (suojin); printf (""); I = NXT [NXT [NXT [I];} else if (s [I] = '<' & S [I + 1]! = '/') {If (suojin) pS (suojin); For (j = I; j } I = NXT [J]; suojin ++ ;} else if (s [I] = '<' & S [I + 1] = '/') {suojin --; PS (suojin ); for (j = I; j T) {puts (""); Return 0;} printf ("\ ncase # % d: \ n", CAS ++) ;}} else {PS (suojin ); for (j = I; j


HDU-4782-Beautiful soup (simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.