POJ - 2271

在天梯赛被L1-8“弄死“”之后练的题目

If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed. 
Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do? 
Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.

Input

The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines. 
A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>.

Output

You should display the the resulting text using this rules: 
If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line. 
If you read a <br> in the input, start a new line. 
If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again). 
The last line is ended by a newline character.

Sample Input

Hallo, dies ist eine 
ziemlich lange Zeile, die in Html
aber nicht umgebrochen wird.
<br>
Zwei <br> <br> produzieren zwei Newlines. 
Es gibt auch noch das tag <hr> was einen Trenner darstellt.
Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
Achtung       mehrere Leerzeichen irritieren

Html genauso wenig wie


mehrere Leerzeilen.

Sample Output

Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
wird.
Zwei

produzieren zwei Newlines. Es gibt auch noch das tag
--------------------------------------------------------------------------------
was einen Trenner darstellt. Zwei
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
genauso wenig wie mehrere Leerzeilen.

题意就是说模拟解析HTML的代码,并把他们重新输出
至于坑点就是在每一行只能有80个字符上
那么上代码,具体思路在代码里有注释
 1 /*
 2 加油,别忘记你写这段话时候的心情
 3 如临深渊,如履薄冰
 4 */
 5 //#include<bits/stdc++.h>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<vector>
 9 #include<algorithm>
10 #include<cmath>
11 #include<iostream>
12 #include<queue>
13 using namespace std;
14 typedef long long ll;
15 typedef unsigned long long ull;
16 const int maxn = 100001;
17 const ll mod = 98244353;
18 
19 
20 int main()
21 {
22     string s;
23     int cnt = 0;   //用cnt来记录现在行数的字符个数,还有一个作用是用来控制输出格式
24     while(cin >> s)
25     {
26         if(s[0] == '\r' || s[0] == '\n') continue;
27         int len = s.size();
28         if(s == "<br>")  //解析<br>
29         {
30             cnt = 0;
31             cout << endl;
32             continue;
33         }
34         if(s == "<hr>")  //解析<hr>
35         {
36             if(cnt)cout << endl;
37             cnt = 0;
38             for(int i = 0; i < 20; i++)
39             {
40                 cout<<"----";
41             }
42             cout << endl;
43             continue;
44         }
45         if(len + cnt +1 == 80)  //分情况讨论,怎样加上当前字符串,加上当前字符串后的操作
46         {
47             if(cnt) cout << " ";
48             cout << s << endl;
49             cnt = 0;
50         }
51         else if(len == 80)
52         {
53             if(cnt == 0)
54             {
55                 cout << s << endl;
56             }
57             else
58             {
59                 cnt = 0;
60                 cout << endl << s << endl;
61             }
62         }
63         else if(len + cnt +1 > 80)
64         {
65             cnt = len;
66             cout << endl << s;
67         }
68         else
69         {
70             if(cnt)
71             {
72                 cout << " ";
73                 cnt++;
74             }
75             cout << s;
76             cnt = cnt + len;
77         }
78     }
79     return 0;
80 }

猜你喜欢

转载自www.cnblogs.com/wxytxdy/p/10655567.html
POJ