HDU 4782 Beautiful Soup 模拟

【思路】

以前遇到这种题,老是想用dfs来写,现在终于知道用栈模拟更方便了 =  =。模拟题,没什么好说的,读懂题意剩下的就是码力的问题了。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF = 100011122;
const double INFF = 1e100;
const double eps = 1e-8;
const int mod = 1000000009;
const int NN = 110;
const int MM = 5000010;
/* ****************** */

void FF_kong(int x)
{
    while(x)
    {
        printf(" ");
        x--;
    }
}

int main()
{
    char c;
    int cas, ee = 0;
    int tail = 0;
    string ss;
    bool tag = true;
    bool need = true;
    bool first;
    scanf("%d",&cas);
    while(1)
    {
        if(tag)
        {
            while(need && (c = getchar()) )
            {
                if(c=='<')
                    break;
            }
            ss = "";
            while( (c = getchar()) )
            {
                if(c=='>')
                    break;
                ss += c;
            }

            if(ss=="html")
            {
                printf("Case #%d:\n",++ee);
            }

            if(ss[ss.size()-1]=='/')
            {
                FF_kong(tail);
                cout<<"<"<<ss<<">"<<endl;
                tag = false;

            }
            else
            {
                if(ss[0]=='/')
                {
                    tail--;
                    FF_kong(tail);
                    cout<<"<"<<ss<<">"<<endl;
                    tag = false;

                }
                else
                {
                    FF_kong(tail);
                    cout<<"<"<<ss<<">"<<endl;
                    tag = false;
                    tail++;

                }
            }

            if(ss=="/html" && ee==cas)
                break;

            ss = "";
        }
        else
        {
            first = true;
            while( (c = getchar()) )
            {
                if(c=='<')
                {
                    if(first)
                    {
                        if(ss.size())
                        {
                            FF_kong(tail);
                            first = false;
                            cout<<ss<<endl;
                            ss = "";
                        }
                    }
                    else
                    {
                        if(ss.size())
                            cout<<" "<<ss;
                        cout<<endl;
                        ss = "";
                    }
                    need = false;
                    tag = true;
                    break;
                }

                if(c==32 || c==9 || c==10)
                {
                    if(ss.size())
                    {
                        if(first)
                        {
                            FF_kong(tail);
                            first = false;
                            cout<<ss;
                        }
                        else
                        {
                            cout<<" "<<ss;
                        }
                    }
                    ss = "";
                }
                else
                    ss += c;
            }
        }
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值