题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <stdio.h>
#include <string.h>
int main() {
    char ch[202] = {0};
    while (scanf("%s", ch) != EOF) {
        int len = strlen(ch);
        int dp[len + 1];
        memset(dp, 0, sizeof(dp));
        //max:记录最大连续数字字串长度,end:记录字串的末尾位置
        //dp[i]:记录前i个字符字串中以第i个字符结尾的数字字串长度
        int max = 0, end = 0;
        for (int i = 1; i <= len; i++) {
            if (ch[i - 1] >= '0' && ch[i - 1] <= '9') {
                dp[i] = dp[i - 1] + 1;
            } else {
                dp[i] = 0;
            }
            if (dp[i] > max) {
                max = dp[i];
            }
        }
        //找到所有最长连续字串位置
        for (int i = 1; i <= len; i++) {
            if (dp[i] == max) {
                end = i;
                for (int i = end - max; i < end; i++) {
                    printf("%c", ch[i]);
                }
            }
        }
        printf(",%d", max);
    }
}
上海得物信息集团有限公司公司福利 1174人发布
查看6道真题和解析