杭电2014年计算机复试真题

注意:此复试题目是根据CSDN博客逃离地球的小小呆上发布的回忆版整理复原。为了恢复试题的原貌,我根据试题要求进行合理的脑补,按照oj系统的风格补全了试题的Problem Description、Input、Output、Sample Input、Sample Out等内容,并加入了详解、具体的代码实现。因为是复原版,不能保证当时的题目就是这么呈现的,也不能说明考场上的试题就是oj风格叙述的,仅供大家参考。有什么错误、不合理的地方欢迎指出。
原创不易,还请大家多支持

第一题

注意:

该题出自HDUOJ 1088,以下对题目的描述都来自于1088题

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.

题目大意:做一个html解析的浏览器。其实就是字符串处理。规则如下:

  1. 每行最多80个字符。超过则换行。另外注意,每个单词不会被截断。就是说如果加上一个单词会超过80字符的话,直接换行,从下一行输出这个单词,而不是截断成两行。
  2. 遇到<br>转化成换行符
  3. 遇到<hr>换行后输出80个字符‘-’。

刚开始我是想用gets来直接读取一行,然后寻找这一行里有没有<br>、<hr>之类的特殊标签。但是,题目中的这些标签都是用空格隔开的,若保留这些空格,就会出现PE错误,而且在<hr>标签的处理上也不方便,就放弃了这种做法。

这道题其实可以算一道排版题。我们通过scanf一个一个单词的读入,而并非读一行。若我们读到的单词是<br>则不管该行有没有数据,直接换行;若读到的单词是<hr>,若改行有数据,则换行并输出分割线’-’、换行,若没有数据则直接输出分割线、换行。这里我们可以定义一个变量来记录改行共有多少个数据(字符)。若我们读到的单词是普通单词且该行是空行,则直接输出该单词。否则输出前导空格后再输出该单词。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
#include<iostream>
using namespace std;
int main() {
char str[100];
int cnt = 0;
while (scanf("%s", str) != EOF) {
if (strcmp(str, "<br>") == 0) {//换行,并重置该行的长度为0
cout << endl;
cnt = 0;
continue;
}
else if (strcmp(str, "<hr>") == 0) {//对于<hr>的处理
if (cnt)//如果该行有数据
cout << endl << "--------------------------------------------------------------------------------" << endl;
else//如果该行没有数据
cout << "--------------------------------------------------------------------------------" << endl;
cnt = 0;
continue;
}
else {
int len = strlen(str);
if (!cnt) {//如果该行是空行
cnt = len;
cout << str;
}
else if (cnt + len + 1 > 80) {//如果改行超过80字符长度,换行。
cout << endl << str;
cnt = len;
}
else {//如果没超过80字符
cout << " " << str;
cnt += len + 1;//+1是需要把空格算上去
}
}
}
if (cnt)//若最后还有数据,还需换行
cout << endl;
return 0;
}

第二题

注意:

该题出自HDUOJ 1020,以下对题目的描述都来自于1020题

Problem Description

Given a string containing only 'A' - 'Z', we could encode it using the following method: 1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string. 2. If the length of the sub-string is 1, '1' should be ignored.
**Input**
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.

Output

For each test case, output the encoded string in a line.

Sample Input

2
ABC
ABBCCC

Sample Output

ABC
A2B3C

题目大意:给定一个仅包含“A”-“Z”的字符串,对其进行编码:每个包含k个字符的子字符串应该被编码为“kX”,其中“X”是这个子字符串中唯一的字符。 如果子字符串的长度为1,则省略前面的1。 这道题很容易让人产生误解,我就当时掉坑里了…~~我把他当成求一个字符串中所有相同的字符了,于是我对这个字符串中的字符先进行了排序然后依次统计所有重复的字符巴拉巴拉…~~这样就直接WA掉了。而题目只要求统计连续相同的字符,这一点要十分注意。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>

int main() {
int n;
while (scanf("%d",&n)!=EOF)
{
char str[10000];
int cnt = 1;//记录重复的次数
while (n > 0)
{
n--;
scanf("%s", &str);
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i] == str[i + 1])//若相邻的两个字符相同
cnt++;
else
{
if (cnt==1)//若重复的次数是1,则省略输出次数
printf("%c", str[i]);
else//若重复次数大于1,则输出次数
{
printf("%d%c", cnt, str[i]);
cnt = 1;//重置次数为1
}
}
}
printf("\n");
}
}
return 0;
}
文章作者: Teily
文章链接: https://teily.cn/article/HDU2014.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 TeilyMa's Blog