[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 }

 

posted @ 2016-09-21 20:21  Kirai  阅读(183)  评论(0编辑  收藏  举报