题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string str; while (getline(cin, str)) { vector<int> dp(str.size() + 1, 0); int maxLen = 0; for (int i = 1; i <= str.size(); ++i) { if (isdigit(str[i - 1])) { dp[i] = dp[i - 1] + 1; maxLen = max(maxLen, dp[i]); } } for (int i = 0; i < dp.size(); ++i) { if (dp[i] == maxLen) { cout << str.substr(i - maxLen, maxLen); } } cout << ',' << maxLen << endl; } }