Give My Text Back
Description:
题目1 : Give My Text BackTo 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:
- Each sentence contains at least one word, begins with a letter and ends with a period.
- In a sentence the only capitalized letter is the first letter.
- In a sentence the words are separated by a single space or a comma and a space.
- 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:
- Changing the cases of letters.
- Adding spaces between words and punctuations.
Given the messed text, can you help Little Ho restore the originaltext?
Input:
A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.
Output:
The original text.
Sample input:
my Name is Little Hi.
His name IS Little ho , We are friends.Sample output:
My name is little hi.
His name is little ho, we are friends.
水题。
分析:
首先弄清楚,text只会发生两种变化:
- 大小写变化。
- word与word之间,或者word与punctuation之间增加space,space只会增加不会减少。所以不会出现
hias.HIS
这样的情况(本来应该是这样hias. His
)。
思路:
- 首先,按行读取数据(这里存成string类型)。可以每读取一行,紧接着对其进行处理;也可以读取所有数据后(存储到
vector<string>
中),遍历每一项并对其处理,这里的代码采用后者。 - 对每一行的数据用空格
' '
进行split,得到vector<string>
存储split后的子串, - 遍历上面得到的
vector<string>
对每一个子串进行分情况处理。
注意:
- 代码行处,对数据分割后得到的字串可能情况有下面几种情况。这里需要特殊对待的是最后一种情况。
如果得到的子串是hias. HIS
,处理后正确的结果应该是hias. His
,但是如果直接分割、小写化后结果是hias. his
。原因是,这里判断首字母是否大写是根据.
判断的,如果遇到了.
就设置下面子串的首字母大写,所以这里需要判断子串中是否存在.
。
倒数第二种情况可以直接将其转换为小写形式后直接append到结果string中,并不会产生影响。""
","
"."
"hELLo"
"hELLo,"
"hELLo."
- 另外,上面是一开始分析漏掉的情况,提交一直wa,wa的那叫一个心痛,所以做题一定分析完全之后下手。
- 这道题目页可以逐个字符的读取,并对其进行处理,这种情况下会相对比本post中提到的情况容易分析和实现。参考代码
代码如下:
1 |
|