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

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

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

#include <cstring>

#include <iostream>

#include<vector>

using namespace std;

int main() {

    string s;

    while(cin >> s)

    {

        vector<pair<int, string>> res;

        int map[201] = {0};

        int max_len = 0;

        int len = s.length();

        if(len == 1) cout << s << 1 << endl;

        int i = 0, j = 0;

        while(i < len && j < len)

        {

            while(!(s[i] >= '0' && s[i] <= '9')) i++;

            j = i;

            while(s[j] >= '0' && s[j] <= '9') j++;

            res.push_back(make_pair(j-i, s.substr(i,j-i)) );

            map[j-i]++;

            i = j;

        }

        for(int i = 200; i >= 0; --i)

            if(map[i] != 0){

                max_len = i;

                break;

            }

        int cnt = map[max_len];

        for(int i = 0; i < res.size(); ++i) if(res[i].first == max_len) cout << res[i].second;

        cout << "," << max_len << endl;

    }    

}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

05-19 15:21
已编辑
华南农业大学 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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