题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

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")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 18:34
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务