题解 | #字符串的展开#

考察模拟,从第二个字符开始判断第i个字符是否为减号,为减号且左右侧字符属于字母或数字其中一种时进行展开(这里显然不用将末位字符为减号的情形作单独处理),否则直接添加至结果字符串;

对于展开函数,按题目所给条件模拟即可。

#include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0)
using namespace std;
string res;

string rep(const char c,int x){
    string s;
    while(x--) s += c;
    return s;
}
bool jg(char a,char b){    //判断是否展开
    if(a < b){
        if((a <= 57)&&(a >= 48)&&(b <= 57)&&(b >= 48)) return true;
        else if((a <= 122)&&(a >= 97)&&(b <= 122)&&(b >= 97)) return true;
        else if((a <= 90)&&(a >= 65)&&(b <= 90)&&(b >= 65)) return true;
        else return false;
    }
    else return false;
}
void proc(int x,int y,int z,char a,char b){    //展开处理
    int d = b - a - 1;
    char ha,la;
    //大小写
    if((a >= 'A')&&(a <= 'Z')){
        ha = a;    la = a + 32;
    }
    else{
        la = a;    ha = a - 32;
    }
    //填充处理
    if(x == 3) res += rep('*',y*d);
    else if((a >= '0')&&(a <= '9')){    //减号两侧是数字字符
        for(int i = 1;i <= d;i++)
            if(z == 1) res += rep(a+i,y);
            else res += rep(b-i,y);
    }
    else{    //最后处理填充字母的情形
        if(x == 1){
            for(int i = 1;i <= d;i++)
            if(z == 1) res += rep(la+i,y);
            else res += rep((la+d+1)-i,y);
        }
        else{
            for(int i = 1;i <= d;i++)
            if(z == 1) res += rep(ha+i,y);
            else res += rep((ha+d+1)-i,y);
        }
    }
}

int main(){
    ios;
    int p1,p2,p3;
    string temp;
    cin>>p1>>p2>>p3>>temp;
    res += temp[0];
    for(int i = 1;temp[i] != '\0';i++){
        if((temp[i] == '-')&&(jg(temp[i-1],temp[i+1])))
            proc(p1,p2,p3,temp[i-1],temp[i+1]);
        else res += temp[i];
    }
    cout<<res<<endl;
    return 0;
}
全部评论

相关推荐

牛客221235529号:土木只用写:土木 男 能吃苦 就好了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务