题解
【基础】字符串展开
1 条题解
-
0
#include <iostream> #include <string> using namespace std; int main() { string s; getline(cin, s); string result = ""; int i = 0; while (i < s.length()) { if (s[i] == '-') { // 检查是否可以展开 if (i > 0 && i < s.length() - 1) { char left = s[i-1]; char right = s[i+1]; // 类型一致:都为数字或都为小写字母 bool is_digit = (left >= '0' && left <= '9') && (right >= '0' && right <= '9'); bool is_lower = (left >= 'a' && left <= 'z') && (right >= 'a' && right <= 'z'); if (is_digit || is_lower) { if (right > left) { // 展开:从 left 到 right for (char c = left + 1; c <= right; ++c) { result += c; } // 跳过 '-' 和 right i += 2; continue; } } } // 不满足条件,保留 '-' result += '-'; } else { result += s[i]; } i++; } cout << result << endl; return 0; }
- 1