HDU1088 Write a simple HTML Browser【字符串处理】【水题】

Write a simple HTML Browser

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8533    Accepted Submission(s): 2402

Problem Description

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.

Source

University of Ulm Local Contest 1999

题目大意:输入一段HTML的文本,然后根据输入的文本,按照HTML的语言格式输出

出来,但是每一行不能超出80个字符,超出的就要换行输出。

注:遇到<br>就要换行,遇到<hr>就要在下一行输入80个‘-‘。

思路:第一遍做的时候边读入边输出,刚开始PE了两次,改了改AC了。之后觉得这样

输入输出毫无审美可言。。。所以改成了先存入一个大的字符串中,再将它输出出来。

这样就美观多了。(ーー゛)。。。

第一遍边输入边输出:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

char s[110];

int main()
{
    int Count = 0;
    while(cin >> s)
    {
        if(strcmp(s,"<br>") == 0)
        {
            cout << endl;
            Count = 0;
            continue;
        }
        if(strcmp(s,"<hr>") == 0)
        {
            if(Count > 0)
            {
                Count = 0;
                cout << endl;
            }

            for(int i = 0; i < 80; ++i)
                cout << '-';
            cout << endl;
            continue;
        }
        int len = strlen(s);
        Count += len;
        if(Count + 1 > 80)
        {
             Count = len;
             cout << endl << s;
        }
        else
        {
            if(Count > len)
                cout << ' ';
            cout << s;
            Count++;
        }
    }
    cout << endl;
    return 0;
}

第二遍先输入存储再输出:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

char s[110];
char a[10010];
int main()
{
    int Count = 0;
    int id = 0;
    while(cin >> s)
    {
        if(strcmp(s,"<br>") == 0)
        {
            a[id++] = '\n';
            Count = 0;
            continue;
        }
        if(strcmp(s,"<hr>") == 0)
        {
            if(Count > 0)
            {
                Count = 0;
                a[id++] = '\n';
            }
            for(int i = 0; i < 80; ++i)
                a[id++] = '-';
            a[id++] = '\n';
            continue;
        }
        int len = strlen(s);
        Count += len;
        if(Count + 1 > 80)
        {
             Count = len;
             a[id++] = '\n';
            for(int i = 0; i < len; i++)
                a[id++] = s[i];
        }
        else
        {
            if(Count > len)
                a[id++] = ' ';
            for(int i = 0; i < len; ++i)
                a[id++] = s[i];
            Count++;
        }
    }
    a[id] = 0;
        cout << a << endl;
    return 0;
}
时间: 2024-09-30 07:12:15

HDU1088 Write a simple HTML Browser【字符串处理】【水题】的相关文章

字符串解密--水题

传纸条 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 传纸条是一种在课堂上传递信息的老方法,虽然现在手机短信和QQ聊天越来越普及,但是手写的信息会让人感到一种亲切感.对许多学生而言,在学校里传递一些私秘性的信息是一种令人兴奋的打发时光的方式,特别是在一些令人厌烦的课堂上. XX 和 YY 经常在自习课的时候传纸条来传递一些私密性的信息.但是他们的座位相隔比较远,传纸条要通过其他人才能到达对方.在传递过程中,难免会有一些好奇心

A Simple Math Problem 矩阵打水题

A Simple Math Problem Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two pos

杭电oj1860:统计字符(字符串hash / 水题)

统计字符 题目链接 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 统计一个给定字符串中指定的字符出现的次数 Input 测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串.注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一.当读到'#'时输入结束,相应的结果不要输出.

HDU 1266 Reverse Number(字符串逆转 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM proble

POJ 2871 A Simple Question of Chemistry(水题)

[题意简述]:后一个数减去前一个数并输出. [分析]:水 //208K 16Ms #include<iostream> using namespace std; int main() { double a; double b; bool flag = false; double ans; while(cin>>a) { if(a==999) { cout<<"End of Output"<<endl; break; } if(!flag)

HDOJ1088 Write a simple HTML Browser 【simulation】

Write a simple HTML Browser Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6917    Accepted Submission(s): 1853 Problem Description If you ever tried to read a html document on a Macintosh, yo

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

Acdream 1111:LSS(水题,字符串处理)

LSS Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStatistic Next Problem Problem Description Time flies, four years passed, colleage is over. When I am about to leave, a xuemei ask me an ACM  problem, but

1001 字符串“水”题

1001: 字符串“水”题 时间限制: 1 Sec  内存限制: 128 MB提交: 210  解决: 39[提交][状态][讨论版] 题目描述 给出一个长度为 n 的字符串(1<=n<=100000),求有多少个连续字串中所有的字母都出现了偶数次. 输入 第一行一个正整数 T,表示数据组数(1 <= T <= 10). 接下来 T 行,每行有一个只包含小写字母的字符串. 输出 每个答案输出满足要求字符串个数.每个答案占一行. 样例输入 3 a aabbcc abcabc 样例输出