Luogu P1098 string expansion (title + code + detailed comments)

Topic link: P1098

The title is described
in the question of "Reading Programs and Writing Results" in the Preliminary Popularization Group. We have given an example of string expansion: If the input string contains words similar to "dh" or "4-8" String, we regard it as a shorthand. When outputting, replace the minus sign with a string of continuously increasing letters or numbers, that is, output the above two substrings as "defgh" and "45678" respectively. In this question, we have made the expansion of the string more flexible by adding some parameter settings. The specific agreement is as follows:

(1) In the following situations, it is necessary to expand the string: in the input string, a minus sign "-" appears, both sides of the minus sign are both lowercase letters or numbers, and in the order of ASCII code, The character to the right of the minus sign is strictly greater than the character to the left.

(2) Parameter p1​: expansion method. When p1​=1, fill in lowercase letters for alphabetic substrings; when p1​=2, fill in uppercase letters for alphabetic substrings. The filling method of the number substring is the same in both cases. When p1​=3, whether it is a letter substring or a number string, it is filled with the same number of asterisks "*" as the number of letters to be filled.

(3) Parameter p2​: the number of repeated filling characters. p2​=k means that the same character should be filled with k consecutively. For example, when p2​=3, the substring "dh" should be expanded to "deeefffgggh". The characters on both sides of the minus sign remain unchanged.

(4) Parameter p3​: whether to change to reverse order: p3=1 means to maintain the original order, p3​=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 p1​=1, p2​=2, p3​=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 delete the minus sign in the middle. For example, "de" should be output as "de", and "3-4" should be output as "34". If the character on the right of the minus sign is less than or equal to the character on the left in the order of ASCII code, keep the minus sign in the middle when outputting, for example: "dd" should be output as "dd", and "3-1" should be output as "3- 1".

The input format
has two lines.

The first line is 3 positive integers separated by spaces, representing the parameters p1​, p2​, p3​ in turn.

The second line is a one-line string consisting only of numbers, lowercase letters and minus sign "-". There are no spaces at the beginning and end of the line.

The output format is
one line, which is the expanded string.

Input and output sample
input #1 copy

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

Output #1 copy

abcsttuuvvw1234556677889s-4zz

Enter #2 to copy

2 3 2
add
output #2 copy

aCCCBBBd-d
description/prompt that
40% of the data satisfies: the string length does not exceed 55

%100% of the data satisfies: 1≤p1​≤3,1≤p2​≤8,1≤p3​≤2. The string length does not exceed 100.
NOIP 2007 Improve the second question

//First vomit : I wanted to write a solution on Luogu, but I can’t submit it (click to submit, no response), I’m sorry QAQ

//This question is mainly about simulation , but there are still a lot of details. The code uses functions such as isdigit, isalpha to determine the type of character

// Thinking : Traverse the string, output non-'-' characters directly, when encountering'-', judge whether to expand or output'-', please refer to the code for specific explanation

#include<bits/stdc++.h>

using namespace std;

int main() {

	  int p1, p2, p3;
      string s;
      cin >> p1 >> p2 >> p3 >> s;

      for (int i = 0; s[i]; i++) {
          if (s[i] != '-') cout << s[i];       //直接输出非'-'字符
          else if (!i || !s[i + 1] || !(isdigit(s[i - 1]) && isdigit(s[i + 1]) || isalpha(s[i - 1]) && isalpha(s[i + 1])) || s[i - 1] >= s[i + 1])  cout << '-';         //输出'-'的情况(第一个或者最后一个是'-'; 左右字符不同类; 左字符 >= 右字符)
          else {                      //需要展开
              int num = s[i + 1] - s[i - 1] - 1;                //先求出要填充的字符种类数(要填充的字符数 = 种类数 * p2)
              if (p1 == 3)               //先考虑特殊情况(用'*'填充)
                  for (int j = 0; j < num * p2; j++)                   //注意是输出num * p2个'*'(坑点)
                      cout << '*';
              else{
                  char c;                     //c是要填充的第一个字符
                  if (p1 == 2 && isalpha(s[i - 1])) c = s[i - 1] - 'a' + 'A' + 1;     //特殊情况:小写字符串中填充大写字母
                  else c = s[i - 1] + 1;              //其他情况都是从 左字符 + 1 开始
                  if (p3 == 1)                         //判断顺序
                      for (int j = 0; j < num; j++)             //顺序
                          for (int k = 0; k < p2; k++)
                              cout << char(c + j);
                  else                                                //逆序
                      for (int j = num - 1; j >= 0; j--)
                          for (int k = 0; k < p2; k++)
                              cout << char(c + j);
              }
          }
      }

      return 0;
  }

//This is the first time I have used Markdown to write a problem solution, some places are not so good!

Guess you like

Origin blog.csdn.net/qq_45472866/article/details/107247232
Recommended