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

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

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

#include <cstdio>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
// 1. 找到所有数值子串
// 2. 统计其长度
// 3. 找到最长的输出
int main() {

    string input;
    input.reserve(210);
    
    while (cin >> input) {

        vector<int> pos_vec;
        int maxlen = 0;

        string sep = R"([\d]+)";
        regex reg(sep);
        sregex_iterator iter(input.cbegin(), input.cend(), reg);
        sregex_iterator end;
        for (; iter != end; ++ iter) {
            if (iter->length() > maxlen) {
                maxlen = iter->length();
                pos_vec.clear();
                pos_vec.push_back(iter->position());
            } else if (iter->length() == maxlen) {
                pos_vec.push_back(iter->position());
            } else {
                ;
            }
        }

        for (int i = 0; i < pos_vec.size(); ++ i) {
            for (int pos = pos_vec.at(i); pos < pos_vec.at(i) + maxlen; ++ pos) {
                putchar(input.at(pos));
            }
        }
        cout << "," << maxlen << endl;
    }




    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
今天 12:17
已编辑
商丘师范学院 Java
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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