题解 | 字符串展开

字符串展开

https://www.nowcoder.com/practice/7fea6add1afa4d91ad71b3a3f4032880

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    int p1,p2,p3;
    cin>>p1>>p2>>p3;
    string s;
    cin>>s;
    vector<int>res;
    for(int i = 0;i<s.size();++i)
    {
        if(s[i] == '-')
        {
            if(i>0&&i<s.size()-1)
            {
                int sta = s[i-1];
                int end = s[i+1];
                if((sta>=48&&sta<=57&&end>=48&&end<=57)||(sta>=97&&sta<=122&&end>=97&&end<=122)){
                    if((end - sta)>1)
                    {
                        if(p1 == 3)
                        {
                            string s1 = string((end-sta-1)*p2,'*');
                            cout<<s1;
                        }else if(p1 == 1)
                        {
                            if(p3 == 1)
                            {
                                string s1 = "";
                                for(int j = sta+1;j<end;++j)
                                {
                                    char a = char(j);
                                    s1+=string(p2,a);
                                }
                                cout<<s1;
                            }else {
                                string s1 = "";
                                for(int j = end-1;j>sta;--j)
                                {
                                    char a = char(j);
                                    s1+=string(p2,a);
                                }
                                cout<<s1;
                            }
                        }else if(p1 == 2)
                        {
                            if(p3 == 1)
                            {
                                string s1 = "";
                                for(int j = sta+1;j<end;++j)
                                {
                                    char a;
                                    if(j>=97&&j<=122)
                                     a = char(j-32);
                                    else
                                      a = char(j);
                                    s1+=string(p2,a);
                                }
                                cout<<s1;
                            }else {
                                string s1 = "";
                                for(int j = end-1;j>sta;--j)
                                {
                                    char a;
                                    if(j>=97&&j<=122)
                                     a = char(j-32);
                                    else
                                      a = char(j);
                                    s1+=string(p2,a);
                                }
                                cout<<s1;
                            }

                        }
                    }else if((end-sta)<=0)
                    {
                        cout<<'-';
                    }else {
                        continue;
                    }
                }else {
                    cout<<'-';
                }
            }else {
                cout<<'-';
            }
        }else {
            cout<<s[i];
        }
    }
    
}

全部评论
我能看懂皮毛了
1 回复 分享
发布于 03-08 20:06 河南
点赞 回复 分享
发布于 03-08 17:57 河南

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务