蓝桥杯 ALGO-110 算法训练 字符串的展开

栏目: 编程工具 · 发布时间: 7年前

内容简介:输入格式输出格式输出只有一行,为展开后的字符串。

在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母获数字串替代其中的减号,即,将上面两个子串分别输出为“defgh”和“45678”。在本题中,我们通过增加一些参数的设置,使字符串的展开更为灵活。具体约定如下:

(1) 遇到下面的情况需要做字符串的展开:在输入的字符串中,出现了减号“-”,减号两侧同为小写字母或同为数字,且按照ASCII码的顺序,减号右边的字符严格大于左边的字符。

(2) 参数p1:展开方式。p1=1时,对于字母子串,填充小写字母;p1=2时,对于字母子串,填充大写字母。这两种情况下数字子串的填充方式相同。p1=3时,不论是字母子串还是数字字串,都用与要填充的字母个数相同的星号“*”来填充。

(3) 参数p2:填充字符的重复个数。p2=k表示同一个字符要连续填充k个。例如,当p2=3时,子串“d-h”应扩展为“deeefffgggh”。减号两边的字符不变。

(4) 参数p3:是否改为逆序:p3=1表示维持原来顺序,p3=2表示采用逆序输出,注意这时候仍然不包括减号两端的字符。例如当p1=1、p2=2、p3=2时,子串“d-h”应扩展为“dggffeeh”。

输入格式

输入包括两行:

第1行为用空格隔开的3个正整数,一次表示参数p1,p2,p3。

输出格式

输出只有一行,为展开后的字符串。

输入输出样例1

输入

输出

1 2 1

abcs-w1234-9s-4zz

输入输出样例2

输入

输出

2 3 2

a-d-d

输入输出样例3

输入

输出

3 4 2

di-jkstra2-6

数据规模和约定

40%的数据满足:字符串长度不超过5

100%的数据满足:1<=p1<=3,1<=p2<=8,1<=p3<=2。字符串长度不超过100

#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
int main() {
    int p1, p2, p3;
    string s;
    cin >> p1 >> p2 >> p3 >> s;
    for (int i = 0; i < s.size(); i++) {
        cout << s[i];
        if (i < s.size() - 2 && s[i + 1] == '-' &&
            (isdigit(s[i]) && isdigit(s[i + 2]) || islower(s[i]) && islower(s[i + 2]))) {
            char c1 = s[i], c2 = s[i + 2];
            string ans;
            if (c1 >= c2) ans = "-";
            else if (c1 + 1 == c2) ans = "";
            else {
                if (p3 == 1) {
                    for (int i = c1 + 1; i < c2; i++) {
                        if (p1 == 1) ans.append(p2, tolower((char) i));
                        if (p1 == 2) ans.append(p2, toupper((char) i));
                        if (p1 == 3) ans.append(p2, '*');
                    }
                }
                if (p3 == 2) {
                    for (int i = c2 - 1; i > c1; i--) {
                        if (p1 == 1) ans.append(p2, tolower((char) i));
                        if (p1 == 2) ans.append(p2, toupper((char) i));
                        if (p1 == 3) ans.append(p2, '*');
                    }
                }
            }
            cout << ans;
            i++;
        }
    }
    return 0;
}
❤❤点击这里 -> 订阅PAT、蓝桥杯、GPLT天梯赛、LeetCode题解离线版❤❤ 蓝桥杯 ALGO-110 算法训练 字符串的展开

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Concepts, Techniques, and Models of Computer Programming

Concepts, Techniques, and Models of Computer Programming

Peter Van Roy、Seif Haridi / The MIT Press / 2004-2-20 / USD 78.00

This innovative text presents computer programming as a unified discipline in a way that is both practical and scientifically sound. The book focuses on techniques of lasting value and explains them p......一起来看看 《Concepts, Techniques, and Models of Computer Programming》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具