hihocoder 1197 Give My Text Back (模拟)
#1197 : 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:
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 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.
比较容易忽视的几个细节是: 连续的空格或者换行符只能有一个;
一个句子是某一行最后一个句子的时候,’.‘后没有空格
比较难处理的是,’.‘或者’,‘前面的空格.
我的做法是,先不处理,最后倒序处理.
1/* ***********************************************
2Author :111qqz
3Created Time :2017年03月31日 星期五 15时45分46秒
4File Name :code/hiho/107A.cpp
5 ************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33string st;
34int main()
35{
36#ifndef ONLINE_JUDGE
37 freopen("code/in.txt","r",stdin);
38#endif
39 while (getline(cin,st))
40 {
41// cout<<st<<endl;
42 int len = st.length();
43 string res="";
44 bool fst = true;
45 bool space = false;
46 bool newl = false;
47 for ( int i = 0 ;i < len ; i++)
48 {
49 if (st[i]!=' ') space = false;
50 if (st[i]!='\t') newl = false; //空格和换行符不能连续.
51 if (fst)
52 {
53 if (st[i]>='a'&&st[i]<='z') res = res + char(st[i]-32),fst = false;
54 else if (st[i]>='A'&&st[i]<='Z') res = res + st[i],fst = false;
55 else if (st[i]==' ')
56 {
57 if (!space)
58 {
59 res = res + " ";
60 space = true;
61 }
62 }
63 else if (st[i]=='\t')
64 {
65 if (!newl)
66 {
67 // res = res + '\t';
68 newl = true;
69 }
70 }
71 continue;
72 }
73 if (!fst)
74 {
75 if (st[i]>='A'&&st[i]<='Z') res = res + char(st[i]+32);
76 else if (st[i]>='a'&&st[i]<='z') res = res + st[i];
77 else if (st[i]==' ')
78 {
79 if (!space&&st[i+1]!=',') //逗号前不能有空格
80 {
81// cout<<"st[i+1]:"<<st[i+1]<<" i:"<<i<<endl;
82 space = true;
83 res = res + " ";
84 }
85 }
86 else if (st[i]=='.')
87 {
88 fst = true;
89 res = res + '.';
90 }
91 else if (st[i]==',') res = res + ',';
92
93
94 }
95 }
96// cout<<res<<endl;
97 len = res.length();
98 bool per = false;
99 string ret="";
100 for ( int i = len-1 ; i>= 0 ; i--)
101 {
102 if (per&&res[i]==' ') continue;
103
104 if (res[i]==','||res[i]=='.') per = true;
105 else per = false;
106 ret = ret + res[i];
107 }
108 reverse(ret.begin(),ret.end());
109 cout<<ret<<endl;
110 }
111
112#ifndef ONLINE_JUDGE
113 fclose(stdin);
114#endif
115 return 0;
116}