zoukankan      html  css  js  c++  java
  • HDU 4782 Beautiful Soup(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?

    pid=4782


    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, except the prettify() method, which attempts to turn a soup into a nicely formatted string. He decides to rewrite the method to prettify a 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 documents (including “tag soup”). It parses “tag soup” into regular HTML documents, 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 documents.
      Tagnames are strings containing only lowercase letters.
      Tags will contain no line break (‘ ’).
      Except 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></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 dizzy 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 documents into formatted parse trees. You should 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 a 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 should 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 </html> tag. See sample below for clarification of the input format.
      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 should write to the output the formatted parse trees as described above. See sample below for clarification of the output format.
     
    Sample Input
    2 <html><body> <h1>ACM ICPC</h1> <p>Hello<br/>World</p> </body></html> <html><body><p> Asia Chengdu Regional</p> <p class="icpc"> ACM-ICPC</p></body></html>
     
    Sample Output
    [pre]Case #1: <html> <body> <h1> ACM ICPC </h1> <p> Hello <br/> World </p> </body> </html> Case #2: <html> <body> <p> Asia Chengdu Regional </p> <p class="icpc"> ACM-ICPC </p> </body> </html> [/pre]
    Hint
    Please be careful of the number of leading spaces of each line in above sample output.
     
    Source


    题意:

    输出一堆乱排版的<html>标签。去多余空字符,转换为按缩进输出。



    代码例如以下:

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    vector<string> vv;
    int main()
    {
        int t;
        int cont, k;
        int cas = 0;
        char ss[1017];
        char c;
        scanf("%d",&t);
        while(t--)
        {
            memset(ss,'',sizeof(ss));
            cont = k = 0;
            vv.clear();
            c = getchar();
            while(1)
            {
                while(c==' ' || c=='
    ' || c=='	')
                    c = getchar();
                if(c != '<')
                {
    //                ss[k++] = c;
    //                if(c == '>')
    //                    c = getchar();
                    while(c!='<'&&c!='
    '&&c!='	'&&c!=' ')
                    {
                        ss[k++] = c;
                        c = getchar();
                    }
                    ss[k] = '';
                    vv.push_back(ss);
                    k = 0;
                    //printf("ss1:%s
    ",ss);
                }
                else
                {
                    ss[k++] = '<';
                    while(c != '>')
                    {
                        c = getchar();
                        ss[k++] = c;
                    }
                    ss[k] = '';
                    vv.push_back(ss);
                    k = 0;
                    if(strcmp(ss,"</html>") == 0)
                        break;
                    c = getchar();
                    //printf("ss2:%s
    ",ss);
                }
            }
    //        for(int i = 0; i < vv.size(); i++)
    //        {
    //            cout<<vv[i]<<endl;
    //        }
            printf("Case #%d:
    ",++cas);
            int flag = 0;
            for(int i = 0; i < vv.size(); i++)
            {
                if(vv[i][0] == '<')
                {
                    flag = 0;
                    if(vv[i][1] != '/')//打开标签
                    {
                        for(int j = 0; j < cont; j++)
                        {
                            printf(" ");
                        }
                        cout<<vv[i]<<endl;
                        int len = vv[i].size();
                        if(vv[i][len-2]!='/')//不是关闭标签
                            cont++;
                    }
                    else//关闭标签
                    {
                        cont--;
                        for(int j = 0; j < cont; j++)
                        {
                            printf(" ");
                        }
                        cout<<vv[i]<<endl;
                    }
                }
                else if(!flag)
                {
                    for(int j = 0; j < cont; j++)
                    {
                        printf(" ");
                    }
                    cout<<vv[i];
                    flag = 1;
                    if(vv[i+1][0] == '<')
                        printf("
    ");
                }
                else if(flag)
                {
                    printf(" ");
                    cout<<vv[i];
                    if(vv[i+1][0] == '<')
                        printf("
    ");
                }
            }
        }
        return 0;
    }


  • 相关阅读:
    ASP.NET中无刷新分页
    Http之基础
    ASP.NET中刷新分页
    SQL之Case when 语句
    在线HTML编辑器
    Highcharts 统计图
    Leetcode练习(Python):数组类:第162题:峰值元素是指其值大于左右相邻值的元素。 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。 你可以假设 nums[-1] = nums[n] = -∞。
    Leetcode练习(Python):数组类:第154题:假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组&#160;[0,1,2,4,5,6,7] 可能变为&#160;[4,5,6,7,0,1,2]&#160;)。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
    Leetcode练习(Python):数组类:第153题:假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组&#160;[0,1,2,4,5,6,7] 可能变为&#160;[4,5,6,7,0,1,2]&#160;)。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
    Leetcode练习(Python):数组类:第152题:给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字)。
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7043662.html
Copyright © 2011-2022 走看看