Gym - 101190A Abbreviation(字符串模拟)

abbreviation.in

This is ACM North Eastern European Regional Contest,
sponsored by International Business Machines.
The. Best. Contest. Ever.
A Great Opportunity for all contestants.

abbreviation.out

This is ACM NEERC (North Eastern European Regional Contest),
sponsored by IBM (International Business Machines).
The. Best. Contest. Ever.
A GO (Great Opportunity) for all contestants.

abbreviation.in

ab Ab A Abc AB Abcd ABc Abcde AbC

abbreviation.out

ab Ab A Abc AB Abcd ABc Abcde AbC

abbreviation.in

Oh  No  Extra Spaces.And,Punctuation Ruin Everything

abbreviation.out

Oh  No  ES (Extra Spaces).And,PRE (Punctuation Ruin Everything)

题目大意:给你几行字符串,每行将连续的符合要求的单词缩写,有以下几个要求:

1.单词需要长度大于1且第一位大写,后几位均为小写。

2.需要至少两个符合条件的单词才可以缩写

3.两个符合的单词之间只能隔一个空格。如果隔了两个及两个以上空格或者标点符号的话,均不满足

思路:凡是这种“单词型”(字符串一段一段,中间有间隔字符的)的模拟题尽量用string,并且在“单词”数量不多的情况下尽量把每个“单词”都提取出来再进行处理。

代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 125;

string S,a[MAXN],b[MAXN];//a中存单词,b中存每个单词后跟的间隔符号。

bool Judge(string x){//判断当前单词是否是符合压缩条件的单词,
    if(x[0]<'A' || x[0]>'Z')return false;
    if(x.length() < 2)return false;
    for(int i=1 ; i<x.length() ; ++i)if(x[i]>='A' && x[i]<='Z')return false;
    return true;
}

int main(){

    freopen("abbreviation.in","r",stdin);
    freopen("abbreviation.out","w",stdout);

    while(getline(cin,S)){
        int tot1,tot2;
        tot1 = tot2 = 0;
        for(int i=0 ; i<S.length() ; ){
            a[++tot1] = "";
            for( ; i<S.length() ; ++i){
                if(isalpha(S[i]))a[tot1] += S[i];
                else break;
            }
            b[++tot2] = "";
            for( ; i<S.length() ; ++i){
                if(!isalpha(S[i]))b[tot2] += S[i];
                else break;
            }
        }
        b[tot2+1] = "";//最后一个单词后为空串。
        string t = "";
        string T = "";
        string re = "";
        int num = 0;
        for(int i=1 ; i<=tot1 ; ++i){
            if(!Judge(a[i])){
                if(num == 1){
                    re += T;
                    T = t = "";
                    num = 0;
                }
                else if(num >= 2){
                    re += t + " (";
                    re += T.substr(0,T.length()-1) + ")" + T[T.length()-1];
                    t = T = "";
                    num = 0;
                }
                re += a[i] + b[i];
            }
            else {
                if(b[i] == " "){
                    t += a[i][0];
                    T += a[i] + b[i];
                    ++num;
                }
                else {
                    t += a[i][0];
                    T += a[i];
                    ++num;
                    if(num == 1){
                        re += T + b[i];
                        T = t = "";
                        num = 0;
                    }
                    else if(num >= 2){
                        re += t + " (";
                        re += T + ")" + b[i];
                        t = T = "";
                        num = 0;
                    }
                }
            }
        }
        cout << re << endl;
    }

    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值