题解 | #在字符串中找出连续最长的数字串# 计数排序思想求最大值

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

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

the code says it all:

#include <bits/stdc++.h>

using namespace std;

const int N = 1010;
string cnt[N];

void solve(string s) 
{
    s += '*';
    string t = "";
    int mxlen = -1;
    for (int i =0; i < s.size(); i++) {
        char c = s[i];
        if (isdigit(c)) {
            t += c;
        }
        else {
            int len = t.size();
            if (len) {
                cnt[len] += t;
                t = "";
                mxlen = max(mxlen, len);
            }
        }
    }
    cout << cnt[mxlen] << ',' << mxlen << endl;
}

int main()
{
    string s;
    for (; getline(cin, s); memset(cnt, 0, sizeof(cnt)))
        solve(s);
    return 0;
}
全部评论
很清晰的解题思路
点赞 回复 分享
发布于 2021-11-24 09:16

相关推荐

A_SOUL_Off...:疑似加班加出幻觉了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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