给定一个字符串 s,请找出其中最长的连续数字串。
如果存在多个长度相同的连续数字串,输出第一个出现的。
输入描述
输入一行字符串 s。
输出描述
输出最长的连续数字串。
示例 1
输入
text
abc1234def56789gh
输出
text
56789
C++ 解法
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
string ans, cur;
for (char c : s) {
if (isdigit(c)) {
cur += c;
} else {
if (cur.size() > ans.size()) ans = cur;
cur.clear();
}
}
if (cur.size() > ans.size()) ans = cur;
cout << ans << endl;
return 0;
}