用C++解题:题目描述:如果在输入的字符串中含有类似于d—h或者4—8的字符子串我们就把它当作一种简写输出时用连续递增的字母或数字串替代其中的减号即将上面两个子串分别输出为defgh和45678。具体约定如下:1遇到下面的情况需要对字符串进行展开:在输入的字符串中出现了减号—减号两侧同为小写字母或同为数字且按照ASCII码的顺序减号右边的字符严格大于左边的字符。2如果减号右边的字符恰好是左边字符的
#include <iostream>
#include <string>
using namespace std;
string expandString(string str) {
string result = "";
int i = 0;
while (i < str.length()) {
if (str[i] == '-') {
// Check if the characters on both sides of '-' are valid
if ((islower(str[i-1]) && islower(str[i+1]) && (str[i+1] > str[i-1]+1)) ||
(isdigit(str[i-1]) && isdigit(str[i+1]) && (str[i+1] > str[i-1]+1))) {
// Expand the characters on both sides of '-'
for (char c = str[i-1]+1; c < str[i+1]; c++) {
result += c;
}
i += 2; // Skip the '-' and the character after '-'
} else {
result += str[i]; // Keep the '-' and continue to the next character
i++;
}
} else {
result += str[i];
i++;
}
}
return result;
}
int main() {
string str;
cin >> str;
string expandedStr = expandString(str);
cout << expandedStr << endl;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/igwz 著作权归作者所有。请勿转载和采集!