• hdu


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

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

    ——>>2013年成都区赛题目。当时挺多做不出最后一题的队伍做出了此题,而我,无限WA到比赛结束。。

    今天。我AC了。。

    题目中有一句话很重要:you shouldn’t change anything of any tag.

    想想规范化后的标签,仅仅有两种方式开头,一种是标签 < 开头,还有一种是文本开头。。每种开头分别相应一种结尾。。

    于是,读标签<xxx>时一直读到标签尾。

    读文本时一直读到文本尾。。

    最后,就AC吧。。

    #include <cstdio>
    #include <cstring>
    
    const int MAXN = 200;
    const char* stop = "</html>";
    
    char ch;
    
    bool IsSpace(char ch)
    {
        return ch == 32 || ch == 9 || ch == 10;
    }
    
    void PrintSpace(int n)
    {
        while (n--)
        {
            putchar(' ');
        }
    }
    
    void RemoveSpace()
    {
        while ((ch = getchar()) && IsSpace(ch));
    }
    
    void Enter()
    {
        putchar('
    ');
    }
    
    void GetEntireTag(char* tag)
    {
        int len = 0;
        tag[len++] = '<';
        while ((ch = getchar()) && ch != '>')
        {
            tag[len++] = ch;
        }
        tag[len++] = '>';
        tag[len] = '';
    }
    
    void OutputTag(const char* tag, const int& spaceCnt)
    {
        if (tag[1] == '/')
        {
            PrintSpace(spaceCnt - 1);
        }
        else
        {
            PrintSpace(spaceCnt);
        }
        puts(tag);
    }
    
    void UpdateSpace(const char* tag, int& spaceCnt)
    {
        int len = strlen(tag);
    
        if (tag[1] != '/' && tag[len - 2] != '/')
        {
            ++spaceCnt;
        }
        else if (tag[1] == '/')
        {
            --spaceCnt;
        }
    }
    
    void GetAndOutputEntireText(const int& spaceCnt)
    {
        PrintSpace(spaceCnt);
        putchar(ch);
        while ((ch = getchar()) && ch != '<')
        {
            if (IsSpace(ch))
            {
                RemoveSpace();
                if (ch == '<') break;
                else
                {
                    PrintSpace(1);
                    putchar(ch);
                }
            }
            else
            {
                putchar(ch);
            }
        }
        Enter();
    }
    
    int main()
    {
        int T, kase = 0;
        char tag[MAXN];
    
        scanf("%d", &T);
        getchar();
        while (T--)
        {
            int spaceCnt = 0;
    
            ch = getchar();
            printf("Case #%d:
    ", ++kase);
            while (true)
            {
                if (IsSpace(ch))
                {
                    RemoveSpace();
                }
                else if (ch == '<')
                {
                    GetEntireTag(tag);
                    OutputTag(tag, spaceCnt);
                    if (strcmp(tag, stop) == 0) break;
                    UpdateSpace(tag, spaceCnt);
                    ch = getchar();
                }
                else
                {
                    GetAndOutputEntireText(spaceCnt);
                }
            }
        }
    
        return 0;
    }
    


  • 相关阅读:
    中国式关系
    太太万岁
    matlab记录运行时间命令
    matlab读xls数据
    matlab,xls转换为mat文件
    matlab里plot设置线形和颜色
    matlab里plot画多幅图像、设置总标题、legend无边框
    matlab显示图像的横纵坐标
    去掉matlab图片空白边缘
    matlab显示原图和灰度直方图
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/8285361.html
Copyright © 2020-2023  润新知