hdu - 4782 - Beautiful Soup(模拟)
题意:输出一堆乱排版的html标签,去多余空字符,转换为按缩进输出。
题目链接: pid=4782">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('\n');
} void GetEntireTag(char* tag)
{
int len = 0;
tag[len++] = '<';
while ((ch = getchar()) && ch != '>')
{
tag[len++] = ch;
}
tag[len++] = '>';
tag[len] = '\0';
} 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:\n", ++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;
}
hdu - 4782 - Beautiful Soup(模拟)的更多相关文章
- HDU 4782 Beautiful Soup --模拟
题意: 将一些分散在各行的HTML代码整理成标签树的形式. 解法: 模拟,具体见代码的讲解. 开始没考虑 '\t' .. 代码: #include <iostream> #include ...
- HDU 4782 Beautiful Soup (模拟+注意细节)
思路就是用栈模拟,不用开实体的栈,直接记一个top指针就行. 说说这题的细节: 1.tag标签里的内容不要动,原样输出.比如<p aa bb cc>,就这样输出就行,不要删空格.题目中说了 ...
- hdu 4782 Beautiful Soupz
模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...
- python爬虫之Beautiful Soup的基本使用
1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...
- 100天搞定机器学习|Day21 Beautiful Soup
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- 爬虫---Beautiful Soup 反反爬虫事例
前两章简单的讲了Beautiful Soup的用法,在爬虫的过程中相信都遇到过一些反爬虫,如何跳过这些反爬虫呢?今天通过知乎网写一个简单的反爬中 什么是反爬虫 简单的说就是使用任何技术手段,阻止别人批 ...
- 使用Beautiful Soup编写一个爬虫 系列随笔汇总
这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...
- 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(1): 基础知识Beautiful Soup
开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful ...
- Python爬虫学习(11):Beautiful Soup的使用
之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...
随机推荐
- [BZOJ2555]SubString LCT+后缀自动机
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3253 Solved: 975[Submit][Status][Di ...
- AC日记——平衡树练习 codevs 4244
4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...
- AC日记——[SCOI2012]喵星球上的点名 bzoj 2754
2754 思路: AC自动机暴力处理匹配: 强大的ac自动机,强大的fail树,强大的map,强大的vector,强大的指针: 代码: #include <map> #include &l ...
- The number of steps(概率dp)
Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one r ...
- Asp.net中的一个判断session是否合法的做法
1 if (Session["UserID"] == "" || Session["UserID"] == null) 2 { 3 ...
- 【转-记】mysql总结
1 | 查询所有数据 select * from Info 查所有数据 select Code,Name from Info 查特定列 2 | 根据条件查 select * from Inf ...
- Python爬链接
# -*- coding: utf-8 -*- """ Created on Wed Jan 11 17:21:54 2017 @author: PE-Monitor & ...
- hdu6162(树链剖分)
hdu6162 题意 给出一颗带点权的树,每次询问一对节点 \((u, v)\),问 \(u\) 到 \(v\) 的最短路径上所有节点权值在 \([c1, c2]\) 区间内的和. 分析 树链剖分,那 ...
- 某考试 T1 lcm
把lcm写成 (a+n)*(b+n) / gcd(a+n,b+n). 因为gcd可以辗转相减,所以就成了gcd(abs(a-b),a+n),一个常量一个变量之间的gcd,我们可以直接把abs(a-b) ...
- [BZOJ3684]大朋友和多叉树
设答案为$f_s$,它的生成函数为$\begin{align*}F(x)=\sum\limits_{i=0}^\infty f_ix^i\end{align*}$,则我们有$\begin{align* ...