HDU4782——Beautiful Soup(模拟)

来源:互联网 发布:软件开发工程师有多累 编辑:程序博客网 时间:2024/06/02 16:02

题目大意

题目给你一些没有经过排版的HTML代码,让你按照一定的格式排版好,去掉多余空格,保持规则的缩进。

题目分析

注意的地方就是’ < /html >’ 和 ‘< html >’ 可能在同一行。
其实题目本身并没有多少技巧,就是锻炼基本的代码功底。刚开始写的时候,写了一坨稀烂的代码,结果出错之后的Debug变得非常困难。所以看似这种随意的代码风格能节省时间,其实维护的成本要高得多得多。后来又重新敲了一遍,发现其实在你有了一个全局的规划之后再动手,代码会更加清晰,出错之后也更易发现。

#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <cstdio>#include <cstdlib>using namespace std;typedef long long LL;const int INF = 0x7fffffff;const int MOD = 1e9 + 7;const int N = 1e5 + 10;char c;int T;int Cas = 2, Len = 0;bool isSpace(char c) {    return c == 32 || c == 9 || c == 10;}void Skip() {    while((c = getchar()) && isSpace(c)) ;}void nextCase() {    if(T == 0) {        puts("");        exit(0);    }    printf("\nCase #%d:", Cas++);    T--;}void newLine() {    putchar('\n');    for(int i = 0; i < Len; i++) putchar(' ');}void printTags() {    string Temp;    while((c = getchar()) && c != '>') Temp += c;    int l = Temp.size();    if(Temp[0] == '/') {        Len--;        newLine();        printf("<%s>", Temp.c_str());    } else if(Temp[l - 1] != '/') {        newLine();        printf("<%s>", Temp.c_str());        Len++;    } else {        newLine();        printf("<%s>", Temp.c_str());    }    if(Len == 0) nextCase();}void printText() {    newLine();    putchar(c);    while((c = getchar()) && c != '<') {        if(isSpace(c)) {            Skip();            if(c == '<') break;            putchar(' ');        }        putchar(c);    }}int main() {#ifdef Tally_Ho    freopen("in.txt", "r", stdin);#endif // Tally_Ho    scanf("%d", &T);    c = getchar();    printf("Case #1:");    T--;    while(true) {        if(isSpace(c)) {            Skip();        } else if(c == '<') {            printTags();            c = getchar();        } else {            printText();        }    }    return 0;}
0 0