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;
}