[HDOJ4782]Beautiful Soup(模拟)

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

题意:给html代码美化一下。

  细节很重要,有一个坑点就是,文本换行的时候有坑,就是会向上缩并且加空格。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <cassert>
  8 #include <cstdio>
  9 #include <bitset>
 10 #include <vector>
 11 #include <deque>
 12 #include <queue>
 13 #include <stack>
 14 #include <ctime>
 15 #include <set>
 16 #include <map>
 17 #include <cmath>
 18 using namespace std;
 19 #define fr first
 20 #define sc second
 21 #define cl clear
 22 #define BUG puts("here!!!")
 23 #define W(a) while(a--)
 24 #define pb(a) push_back(a)
 25 #define Rint(a) scanf("%d", &a)
 26 #define Rll(a) scanf("%I64d", &a)
 27 #define Rs(a) scanf("%s", a)
 28 #define Cin(a) cin >> a
 29 #define FRead() freopen("in", "r", stdin)
 30 #define FWrite() freopen("out", "w", stdout)
 31 #define Rep(i, len) for(int i = 0; i < (len); i++)
 32 #define For(i, a, len) for(int i = (a); i < (len); i++)
 33 #define Cls(a) memset((a), 0, sizeof(a))
 34 #define Clr(a, x) memset((a), (x), sizeof(a))
 35 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
 36 #define lrt rt << 1
 37 #define rrt rt << 1 | 1
 38 #define pi 3.14159265359
 39 #define RT return
 40 #define lowbit(x) x & (-x)
 41 #define onecnt(x) __builtin_popcount(x)
 42 typedef long long LL;
 43 typedef long double LD;
 44 typedef unsigned long long ULL;
 45 typedef pair<int, int> pii;
 46 typedef pair<string, int> psi;
 47 typedef pair<LL, LL> pll;
 48 typedef map<string, int> msi;
 49 typedef vector<int> vi;
 50 typedef vector<LL> vl;
 51 typedef vector<vl> vvl;
 52 typedef vector<bool> vb;
 53
 54 const int maxn = 20200;
 55 const char* endx = "</html>";
 56 char tmp;
 57 char s[maxn];
 58 char tag[maxn];
 59 int depth, n;
 60 bool ex = 0;
 61
 62 void p(int x) {
 63   Rep(i, x) printf(" ");
 64 }
 65
 66 void f() {
 67   n = 0;
 68   while(tmp != ‘>‘) {
 69     s[n++] = tmp;
 70     tmp = getchar();
 71   }
 72   s[n++] = ‘>‘;
 73   s[n] = 0;
 74   if(s[1] == ‘/‘) {
 75     depth -= 1;
 76     p(depth);
 77   }
 78   else {
 79     p(depth);
 80     if(s[n-2] != ‘/‘) depth += 1;
 81   }
 82   if(strstr(s, endx) != NULL) ex = 1;
 83   printf("%s\n", s);
 84 }
 85
 86 void g() {
 87   bool first = 1;
 88   p(depth);
 89   while(1) {
 90     if(!first) p(1);
 91     while(tmp != ‘ ‘&& tmp != ‘\n‘ && tmp != ‘\t‘ && tmp != ‘<‘) {
 92       putchar(tmp); tmp = getchar();
 93     }
 94     first = 0;
 95     while(tmp == ‘ ‘ || tmp == ‘\n‘ || tmp == ‘\t‘) tmp = getchar();
 96     if(tmp == ‘<‘) {
 97       puts("");
 98       f();
 99       break;
100     }
101   }
102 }
103
104 signed main() {
105   //FRead();
106   int T, _ = 1;
107   Rint(T);
108   W(T) {
109     printf("Case #%d:\n", _++);
110     depth = 0; ex = 0;
111     while(1) {
112       if(ex) break;
113       tmp = getchar();
114       while(tmp == ‘ ‘ || tmp == ‘\n‘ || tmp == ‘\t‘) tmp = getchar();
115       if(tmp == ‘<‘) f();
116       else g();
117     }
118   }
119   RT 0;
120 }
时间: 2024-08-06 07:57:11

[HDOJ4782]Beautiful Soup(模拟)的相关文章

hdu4872 Beautiful Soup 模拟

Beautiful Soup Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1912    Accepted Submission(s): 391 Problem Description Coach Pang has a lot of hobbies. One of them is playing with “tag soup” wit

HDU 4782 Beautiful Soup(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4782 Problem Description Coach Pang has a lot of hobbies. One of them is playing with "tag soup" with the help of Beautiful Soup. Coach Pang is satisfied with Beautiful Soup in every respect, except

爬虫---Beautiful Soup 爬取知乎热榜

前两章简单的讲了Beautiful Soup的用法,在爬虫的过程中相信都遇到过一些反爬虫,如何跳过这些反爬虫呢?今天通过豆瓣网写一个简单的反爬中 什么是反爬虫 简单的说就是使用任何技术手段,阻止别人批量获取自己网站信息的一种方式.关键也在于批量. 反反爬虫机制 增加请求头---headers为了模拟更真实的用户场景 更改IP地址---网站会根据你的IP对网站访问频密,判断你是否属于爬虫 ua限制---UA是用户访问网站时候的浏览器标识,其反爬机制与ip限制类似 模拟帐号登录----通过reque

[Python]HTML/XML解析器Beautiful Soup

[简介] Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.即HTML/XMLX的解析器. 它可以很好的处理不规范标记并生成剖析树(parse tree). 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作.它可以大大节省你的编程时间. [安装] 下载地址:点击打开链接 Linux平台安装: 如果你用的是新版的Debain或ubuntu,那么可以通过系统的软件包管理来安装: $ apt-get install Python-bs4 B

python标准库Beautiful Soup与MongoDb爬喜马拉雅电台的总结

Beautiful Soup标准库是一个可以从HTML/XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式,Beautiful Soup将会节省数小时的工作时间.pymongo标准库是MongoDb NoSql数据库与python语言之间的桥梁,通过pymongo将数据保存到MongoDb中.结合使用这两者来爬去喜马拉雅电台的数据... Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是

HDU-4782-Beautiful Soup(模拟)

Problem Description Coach Pang has a lot of hobbies. One of them is playing with "tag soup" with the help of Beautiful Soup. Coach Pang is satisfied with Beautiful Soup in every respect, except the prettify() method, which attempts to turn a sou

爬虫学习——网页解析器Beautiful Soup

一.Beautiful Soup的安装与测试 官方网站:https://www.crummy.com/software/BeautifulSoup/ Beautiful Soup安装与使用文档:  https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 1.首先测试一下bs4模块是否已经存在,若不存在再安装即可,我用的是kali测试发现bs4模块已经存在,下面介绍如何测试与安装 新建python文档输入以下代码 1 import bs4 2

Beautiful Soup的使用

Beautiful Soup简单实用,功能也算比较全,之前下载都是自己使用xpath去获取信息,以后简单的解析可以用这个,方便省事. Beautiful Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树.通常用来分析爬虫抓取的web文档.对于 不规则的 Html文档,也有很多的补全功能,节省了开发者的时间和精力. Beautiful Soup 的官方文档齐全,将官方给出的例子实践一遍就能掌握.官方英文文档,中文文档 一 安装 Beauti

2017.08.11 Python网络爬虫实战之Beautiful Soup爬虫

1.与Scrapy不同的是Beautiful Soup并不是一个框架,而是一个模块:与Scrapy相比,bs4中间多了一道解析的过程(Scrapy是URL返回什么数据,程序就接受什么数据进行过滤),bs4则在接收数据和进行过滤之间多了一个解析的过程,根据解析器的不同,最终处理的数据也有所不同,加上这一步骤的优点是可以根据输入数据的不同进行针对性的解析:同一选择lxml解析器: 2.安装Beautiful Soup环境:pip install beautifulsoup4 3.Beautiful