hdu 4782 Beautiful Soup 2013成都区域赛 大模拟

题目

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

题目来源:2013成都区域赛第五题的样子。

简要题意:具体看题目,基本上就是写个html formatter

数据范围:20KB

实现

自己开比赛练的时候比较浪,这题写得也不是很用心,错了好多次,题意也有点不明。

然后怒用工程风格重写一发,1A,爽,模拟题之类的风格好,工程做得多是有帮助的。

其实也没特意去写个类封装起来,不过函数分解得挺干净了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1e5+5;
char s[N];

char ch = ' ';

int num = 0;

bool isBlank(char ch) {
    return ch == '\t' || ch == ' ' || ch == '\n';
}

char skipBlank() {
    while (isBlank(ch)) ch = getchar();
    return ch;
}

void printBlank(int blankNum) {
    for (int i = 0; i < blankNum; i++) putchar(' ');
}

void printTag(int blankNum, int len) {
    printBlank(blankNum);
    printf("%s\n", s);
}

int parseTag() {
    int len = 0;
    do {
        s[len++] = ch;
        ch = getchar();
    } while (s[len-1] != '>');
    s[len] = '\0';
    return len;
}

int parseString() {
  int len = 0;
  while (!isBlank(ch) && ch != '<') {
        s[len++] = ch;
        ch = getchar();
  }
  s[len] = '\0';
  return len;
}

void handleString() {
    printBlank(num-1);
    do {
        parseString();
        printf(" %s", s);
    } while (skipBlank() != '<');
    puts("");
}

bool handleTag() {
    int len = parseTag();
    if (s[1] == '/') {
        printTag(--num, len);
        if (strcmp(s, "</html>") == 0) return true;
    } else if (s[len-2] != '/') {
        printTag(num++, len);
    } else {
        printTag(num, len);
    }
    return false;
}

void parse() {
    while (1) {
        if (skipBlank() == '<') {
            if (handleTag()) return;
        } else {
            handleString();
        }
    }
}

int main()
{
    int t, cas = 1;
    scanf("%d", &t);
    while (t--) {
        printf("Case #%d:\n", cas++);
        parse();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值