题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <iostream>
#include <vector>
using namespace std;
struct node {
int length;
string s;
};
int main() {
string str;
while(cin>>str) {
vector<node> vec;
string tmp;
int len=0,maxlen=0,size=0;
for(char it:str){
size++;
if(it-0<'0'-0 || it-0>'9'-0){
if(!tmp.empty())
vec.push_back({len, tmp});
maxlen=max(len,maxlen);
tmp.clear();
len=0;
continue;
}
tmp+=it;
len++;
if(size==str.size()){
if(!tmp.empty())
vec.push_back({len, tmp});
maxlen=max(len,maxlen);
tmp.clear();
len=0;
}
}
for(node it: vec) {
if(it.length == maxlen) {
cout<<it.s;
}
}
cout<<","<<maxlen<<endl;
}
}
// 64 位输出请用 printf("%lld")
查看30道真题和解析