洛谷 字符串的展开

题号P1098

读好题最重要!一定要按照题目要求理清程序架构!否则会出问题!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int p1,p2,p3;
string s;
void reverse_str(string & s){
    int t=s.length();
    for(int i=0;i<t/2;i++){
        swap(s[i],s[t-1-i]);
    }
}
string getStr(char left,char right){
    string s="";
    if(left<right&&(isdigit(left)&&isdigit(right)||isalpha(left)&&isalpha(right))){
        if(right==left+1)return "";

        //填充数字
        if(isdigit(left)&&isdigit(right)){
            for(char c=left+1;c<right;c++){
                s.append(p2,c);
            }
        }else if(isalpha(left)&&isalpha(right)){
            for(char c=left+1;c<right;c++){
                if(p1==1||p1==3)s.append(p2,tolower(c));
                else if(p1==2)s.append(p2,toupper(c));
            }
        }
        //逆序、星号
        if(p1==3){
            int leng=s.length();
            for(int i=0;i<leng;i++){
                s[i]='*';
            }
            return s;
        }
        if(p3==2){
            reverse_str(s);    
        }
        return s;

    }else return "-";
}
int main(){
    cin>>p1>>p2>>p3;
    cin>>s;
    int pos=-1;
    while((pos=s.find('-',pos+1))!=string::npos){
        char l=s[pos-1],r=s[pos+1];
        s.erase(pos,1);
        string s1=getStr(l,r);
        s.insert(pos,s1);
    }
    cout<<s;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注