#include <iostream>
#include <string>
std::string findLongestIncreasingSubstr(const std::string& str) {
std::string longestSubstr = "";
std::string currentSubstr = "";
for (size_t i = 0; i < str.length(); i++) {
// 如果当前字符是数字,并且与前一个字符递增,则将其添加到当前子串中
if (i > 0 && isdigit(str[i]) && str[i] - str[i - 1] == 1) {
if (currentSubstr.empty()) {
currentSubstr += str[i - 1];
}
currentSubstr += str[i];
} else {
// 否则,比较当前子串与最长子串的长度,并更新最长子串
if (currentSubstr.length() > longestSubstr.length()) {
longestSubstr = currentSubstr;
}
// 重置当前子串
currentSubstr = "";
}
}
// 比较最后一个子串与最长子串的长度,并更新最长子串
if (currentSubstr.length() > longestSubstr.length()) {
longestSubstr = currentSubstr;
}
return longestSubstr;
}
int main() {
std::string str = "abcd123456";
std::string longestSubstr = findLongestIncreasingSubstr(str);
std::cout << "Longest increasing substring: " << longestSubstr << std::endl;
return 0;
}
05-19
3928
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-31
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交