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

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

https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d

#include <cctype>
#include <csetjmp>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    string s;
    cin>>s;
    int n = s.size();
    vector<int> dp(n+1,0);
    s = ' '+s;
    pair<int,int> max_len;
    for (int i=1; i< n+1;i++) {
        if(isdigit(s[i]) && !isdigit(s[i-1])){
            dp[i] = 1;
        }else if (isdigit(s[i]) && isdigit(s[i-1])) {
            dp[i] = dp[i-1] + 1;
        }else {
            dp[i] = 0;
        }
        if (dp[i]>max_len.first) {
            max_len.first = max(max_len.first,dp[i]);
            max_len.second = i;
        }
        
    }
    string t = s.substr(max_len.second-max_len.first+1,max_len.first);
    cout << t<< endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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