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:

  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.

比较容易忽视的几个细节是: 连续的空格或者换行符只能有一个;

一个句子是某一行最后一个句子的时候,'.'后没有空格

比较难处理的是,'.'或者','前面的空格.

我的做法是,先不处理,最后倒序处理.

/* ***********************************************
Author :111qqz
Created Time :2017年03月31日 星期五 15时45分46秒
File Name :code/hiho/107A.cpp
 ************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6string st;
 7int main()
 8{
 9#ifndef  ONLINE_JUDGE 
10    freopen("code/in.txt","r",stdin);
11#endif
12    while (getline(cin,st))
13    {
14//	cout<<st<<endl;
15	int len = st.length();
16	string res="";
17	bool fst = true;
18	bool space = false;
19	bool newl = false;
20	for ( int i = 0 ;i < len ; i++)
21	{
22	    if (st[i]!=' ') space = false;
23	    if (st[i]!='\t') newl = false; //空格和换行符不能连续.
24	    if (fst)
25	    {
26		if (st[i]>='a'&&st[i]<='z') res = res + char(st[i]-32),fst = false;
27		else if (st[i]>='A'&&st[i]<='Z')  res = res + st[i],fst = false;
28		else if (st[i]==' ')
29		     {
30			 if (!space)
31			 {
32			     res = res + " ";
33			     space = true;
34			 }
35		     }
36		     else if (st[i]=='\t')
37			  {
38			      if (!newl)
39			      {
40			    	 // res = res + '\t';
41				  newl = true;
42			      }
43			  }
44		continue;
45	    }
46	    if (!fst)
47	    {
48		if (st[i]>='A'&&st[i]<='Z') res = res + char(st[i]+32);
49		else if (st[i]>='a'&&st[i]<='z') res = res + st[i];
50		else if (st[i]==' ')
51		{
52		    if (!space&&st[i+1]!=',') //逗号前不能有空格
53		    {
54//			cout<<"st[i+1]:"<<st[i+1]<<" i:"<<i<<endl;
55			space = true;
56			res = res + " ";
57		    }
58		}
59		else if (st[i]=='.') 
60		{
61		    fst = true;
62		    res = res + '.';
63		}
64		else if (st[i]==',') res = res + ',';
1	    }
2	}
3//	cout<<res<<endl;
4	 len = res.length();
5	bool per = false;
6	string ret="";
7	for ( int i = len-1 ; i>= 0 ; i--)
8	{
9	    if (per&&res[i]==' ') continue;
1	    if (res[i]==','||res[i]=='.') per = true;
2	    else  per = false;
3	    ret = ret + res[i];
4	}
5	reverse(ret.begin(),ret.end());
6	cout<<ret<<endl;
7    }
1#ifndef ONLINE_JUDGE  
2	fclose(stdin);
3#endif
4	return 0;
5}