hiho一下 第一百零七周 Give My Text Back(微软笔试题)

题目1 : Give My Text Back

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

To prepare for the English exam Little Ho collected many digital reading materials. Unfortunately the materials are messed up by a malware.

It is known that the original text contains only English letters (a-zA-Z), spaces, commas, periods and newlines, conforming to the following format:

1. Each sentence contains at least one word, begins with a letter and ends with a period.

2. In a sentence the only capitalized letter is the first letter.

3. In a sentence the words are separated by a single space or a comma and a space.

4. The sentences are separated by a single space or a single newline.

It is also known the malware changes the text in the following ways:

1. Changing the cases of letters.

2. Adding spaces between words and punctuations.

Given the messed text, can you help Little Ho restore the original text?

输入

A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.

输出

The original text.

样例输入
my Name  is Little   Hi.
His name IS Little ho , We are friends.
样例输出
My name is little hi.
His name is little ho, we are friends.

题目大意

小Hi和小Ho为了准备英语的期末考试,找了很多英语的电子资料。但是由于U盘出了问题,导致资料内容变得很乱。于是小Hi和小Ho决定写一个程序去将所有的电子资料格式化。 已知每一份资料只包含大小写字母,‘ ’(空格), ‘,’(逗号),‘.’(句号)以及换行符。小Hi和小Ho希望整理后的资料为如下格式: - 每一句话都是以’.’结尾,每一段话都是以换行符结尾。 - 每一段开始没有空格。 - 每一个句子都是完整的,即至少包含1个单词,句末一定为‘.’(句号)。 - 每一句话只有首字母大写。 - 每句话内单词之间由1个空格隔开。 - 标点符号与前面的单词之间无空格,标点符号后有1个空格或换行符。 对于给定的资料,请你对其进行格式化,并输出格式化的结果。

解题思路:

讲每行输入的数据先全部小写化处理,然后检测,空格,连续的空格就行删除,只留一个,逗号和句号后面加空格,因为需要对数组进行直接插入删除操作,所以用vector<char>,,,为什么不用list,因为list链表分配的不是连续的地址,不能通过下标直接存取。代码本地通过了,不过,提交没有AC,只能等下周答案出来了,看看别人的程序,再做修改。

 #include "iostream"
#include "string"
#include "vector" using namespace std; int main()
{
char s[];
bool point_tag = false; while (cin.getline(s, ))
{
int i = -,len;
vector<char> input; while (s[i++])
{
s[i] = tolower(s[i]);
}
len = i;
i = ;
s[i] = toupper(s[i]); for (int i = ; i < len; i++)
{
input.insert(input.begin() + i, s[i]);
} for (int i = ; i < input.size(); i++)
{
if (input[i] == ' ')
while (input[i + ] == ' ' || input[i + ] == ',')
{
if (input[i] == ',') {
input.insert(input.begin() + i + , ' ');
break;
} else
input.erase(input.begin() + i);
}
else if (input[i] == '.')
{
input.insert(input.begin() + i + , ' ');
char c = input[i + ];
c = toupper(c); input.erase(input.begin() + i + );
input.insert(input.begin() + i + , c);
}
} for (int i = ; i < input.size(); i++)
{
if (input[i] == '.')
{
char c = input[i + ];
c = toupper(c); input.erase(input.begin() + i + );
input.insert(input.begin() + i + , c);
}
cout << input[i];
}
cout << endl;
}
}

不能AC的痛!

上一篇:TCP/IP协议族各层的作用


下一篇:【hiho一下 第十周】后序遍历