题目
题目链接: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;
}