您的位置:首页 > 编程语言 > C语言/C++

hihocoder:Give My Text Back

2016-07-19 12:42 302 查看
一、题目

时间限制: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.


二、思路

本题是字符串输入输出的处理。

三、代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> input;
string word = "";
char c;
while (cin >> noskipws >> c){
if (isalpha(c)){
if (c >= 'A'&&c <= 'Z')
c += 32;
word += c;
}
else{
if (word != ""){
input.push_back(word);
word = "";
}
if (c != ' ')
input.push_back(to_string(c));
}
}
int n = input.size();
bool isFirst = true;
for (int i = 0; i < n; i++){
if (input[i] == "44")
cout << ',';
else if (input[i] == "46"){
cout << '.';
isFirst = true;
}
else if (input[i] == "10")
cout << endl;
else{
if (i>0 && input[i - 1] != "10")
cout << " ";
if (isFirst){
input[i][0] -= 32;
isFirst = false;
}
cout << input[i];
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ hihocoder