Luogu C++ language | P1098 Expansion of strings

Learn C++ from a young age! Record the questions asked during the Luogu C++ learning and exam preparation process, and record every moment.

Attached is a summary post: Luogu’s C++ language | Summary_The blog of a communicator who loves programming-CSDN blog


[Title description]

In the question of "reading programs and writing results" in the preliminary popularization group, we gave an example of string expansion: if the input string contains a string similar to dh or 4-8, we will As a shorthand, when outputting, replace the minus sign with a continuously increasing string of letters or numbers, that is, output the above two substrings as defgh and 45678 respectively. In this question, we make the expansion of strings more flexible by adding some parameter settings. The specific agreement is as follows:

(1) When encountering the following situation, you need to expand the string: in the input string, a minus sign - appears, both sides of the minus sign are either lowercase letters or numbers, and in the order of the ASCII code, the minus sign The characters on the right are strictly larger than the characters on the left.

(2) Parameter  p 1: expansion mode. When p 1=1, for the letter substring, fill in the lowercase letters; when p 1=2, for the letter substring, fill in the uppercase letters. The numeric substring is filled in the same way in both cases. When p 1=3, whether it is a letter substring or a numeric string, it is filled with the same number of asterisks * as the number of letters to be filled.

(3) Parameter  p 2: The number of repetitions of fill characters. p 2= k  means that the same character needs to be filled  k  times continuously. For example, when  p 2 = 3, the substring dh should be expanded to deeefffgggh. The characters on both sides of the minus sign are unchanged.

(4) Parameter  p 3: Whether to change to reverse order: p 3=1 means to maintain the original order, p 3=2 means to output in reverse order. Note that the characters at both ends of the minus sign are still not included at this time. For example, when  p 1=1, p 2=2, p 3=2, the substring dh should be expanded to dggffeeh.

(5) If the character to the right of the minus sign happens to be the successor of the character on the left, only the minus sign in the middle is deleted. For example: de should be output as de, and 3-4 should be output as 34. If the characters to the right of the minus sign are less than or equal to the characters on the left in ASCII code order, the minus sign in the middle should be retained when outputting. For example: dd should be output as dd, and 3-1 should be output as 3-1.

【enter】

There are two lines in total.

The first line contains three positive integers separated by spaces, representing the parameters  p 1, p 2, and p 3 in sequence.

Line 2 is a string consisting only of numbers, lowercase letters, and the minus sign -. There are no spaces at the beginning or end of the line.

【Output】

There is one line in total, which is the expanded string.

【Input sample】

1 2 1
abcs-w1234-9s-4zz

【Output sample】

abcsttuuvvw1234556677889s-4zz

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int p1, p2, p3;
string s;
//判定a和b是否同时为数字|字母
bool judge(char a, char b)
{
    if (isdigit(a) && isdigit(b)) {
        return true;
    } else if (isalpha(a) && isalpha(b)) {
        return true;
    }
    return false;
}
void exp(char a, char b)
{
    if (p3==1) {
        for (char i=a+1; i<b; i++) {
            for (int j=0; j<p2; j++) {
                if (p1==1) printf("%c", tolower(i));
                else if (p1==2) printf("%c", toupper(i));
                else printf("*");
            }
        }
    } else {
        for (char i=b-1; i>a; i--) {
            for (int j=0; j<p2; j++) {
                if (p1==1) printf("%c", tolower(i));
                else if (p1==2) printf("%c", toupper(i));
                else printf("*");
            }
        }
    }
}
int main()
{
    cin >> p1 >> p2 >> p3 >> s;
    for (int i=0; i<s.size(); i++) {
        if (s[i]=='-') {
            if (s[i+1]-s[i-1]==1) continue;
            else if (s[i+1] <= s[i-1]) {
                cout << s[i];
                continue;
            }
            else if (judge(s[i-1], s[i+1])) {
                exp(s[i-1], s[i+1]);
                continue;
            }
        }
        cout << s[i];
    }
    return 0;
}

【operation result】

1 2 1
abcs-w1234-9s-4zz
abcsttuuvvw1234556677889s-4zz

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133088911
Recommended