题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <algorithm>
#include <vector>
using namespace std;
int main() {
string str;
while(getline(cin,str)){
string temp;
int max = -1;
int len = 0;
vector<string> v;
for(int i = 0;i<str.size();i++){
while(isdigit(str[i])){
len++;
temp+=str[i];
i++;
}
if(len>=max){
max=len;
v.push_back(temp);
}
len=0;
temp.clear();
}
stable_sort(v.begin(),v.end(),[](const string&lhs,const string&rhs){
return lhs.size()>rhs.size();
});
int end=v.size()-1;
for(int i =0;i<v.size()-1;i++){
if(v[i].size()>v[i+1].size()) {end = i;break;}
}
int size = v[end].size();
for(int i = 0;i<=end;i++){
cout<<v[i];
}
cout<<','<<size<<endl;
}
}