hiho一下第107周《Give My Text Back》

题目链接:传送门

题目大意:给你多组格式不标准的字符串句子,要你恢复到标准格式(单词中间只有一个空格或者一个逗号一个空格,句子的首字母大写其他小写,遇到回车也要换行)

题目思路:直接按题意模拟,注意几个标记变量的状态改变就行

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include<functional>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 30005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL;

char ch;
string st="";

int main(){
    int i,j,x,y;
    int dax=1,kong=0;
    while(cin>>noskipws>>ch){
        if(isalpha(ch)){
            ch=tolower(ch);
            if(dax){
                if(kong)st+=" ";
                st+=toupper(ch);
                dax=0;
            }
            else{
                st+=ch;
            }
            kong=1;
        }
        else{
            if(ch==','){
                if(st[st.length()-1]==' ')st[st.length()-1]=',';
                else st+=',';
                st+=" ";
                kong=0;
            }
            else if(ch=='.'){
                if(st[st.length()-1]==' ')st[st.length()-1]='.';
                else st+='.';
                kong=1;
                dax=1;
            }
            else if(ch==' '||ch=='\t'){
                if(kong){
                    st+=" ";
                    kong=0;
                }
            }
            else if(ch=='\n'){
                if(st!=""){
                    int len=st.length();
                    for(i=len-1;i>=0;--i)if(st[i]!=' ')break;
                    st=st.substr(0,i+1);
                    cout<<st<<endl;
                    dax=1;kong=0;
                    st="";
                }
            }
        }
    }
    return 0;
}

 

posted @ 2016-07-17 16:04  Kurokey  阅读(212)  评论(0编辑  收藏  举报