HJ92题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
vector<string> GetMaxSubDigitStr(string &s)
{
vector<string> res;
int left = 0, right = 0;
int maxSubDigitCnt = 0;
for (int i = 0; i < s.size(); i++){
int digitCnt = 0;
if (!isdigit(s[i])) {
continue;
}
digitCnt++;
int right = i + 1;
for (int j = i + 1; j < s.size(); j++) {
if (isdigit(s[j])) {
digitCnt++;
right = j;
} else {
//right = j + 1;
break;
}
}
if (digitCnt == maxSubDigitCnt) {
res.push_back(s.substr(i, right - i + 1));
}
if (digitCnt > maxSubDigitCnt) {
res.clear();
res.push_back(s.substr(i, right - i + 1));
maxSubDigitCnt = digitCnt;
}
}
return res;
}
int main() {
string s;
while (cin >> s) { // 注意 while 处理多个 case
auto res = GetMaxSubDigitStr(s);
string resStr = "";
for (auto item : res) {
resStr += item;
}
cout << resStr << "," << res[0].size() << endl;
}
}

