首页 > 试题广场 >

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

[编程题]在字符串中找出连续最长的数字串
  • 热度指数:138493 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)
本题含有多组样例输入。

数据范围:字符串长度 , 保证每组输入都至少含有一个数字

输入描述:

输入一个字符串。1<=len(字符串)<=200



输出描述:

输出字符串中最长的数字字符串和它的长度,中间用逗号间隔。如果有相同长度的串,则要一块儿输出(中间不要输出空格)。

示例1

输入

abcd12345ed125ss123058789
a8a72a6a5yy98y65ee1r2

输出

123058789,9
729865,2

说明

样例一最长的数字子串为123058789,长度为9
样例二最长的数字子串有72,98,65,长度都为2    
思路:
1.遍历str,将每个非数字字符赋值'\0',这样就分割出多段数字字符串了
2.遍历数字字符串,使用而外数组,记录每段的起始位置i该段的长度,同时可以记录长度的最大值max
3. 遍历存结果的那个数组,每个值==max,就打印即可。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int count = 0;
    int i, len;
    char str[201]= {0};
    char max_candi[201] = {0};
    while (scanf("%s", str) != EOF) {

        len = strlen(str);
        char *p = str;
        /* 分割字符串 */
        for(i = 0; i < len; i++)
        {
            if (p[i] < '0' || p[i] > '9') {
                p[i] = '\0';
            }
        }

        /* 遍历数字字符串,记录每段的起始位置i和该段的长度 */
        int max = 0;
        for(i = 0; i < len; i++)
        {
            if (p[i] >= '0' && p[i] <= '9') {
                max_candi[i] = strlen(p+i);//易错点
                max = (max > max_candi[i]) ? max : max_candi[i];
                i+=strlen(p+i) - 1;//易错点
            }
        
            
        }
        /* 遍历输出 */
        for(i = 0; i < len; i++)
        {
            if(max_candi[i] == max)
            {
                printf("%s", p+i);
            }
        }
        printf(",%d\n", max);
        memset(str,0,201*sizeof(char));
    }
    return 0;
}


发表于 2023-12-03 10:20:12 回复(0)
#include <stdio.h>
#include <string.h>

int main()
{
    char str[200]={0}, num[200]={0};
    int i, j, k, count, max;

    while(scanf("%s", &str)!=EOF)       
    {
        i=0;
        max=0;
        k=0;
        while(i<strlen(str))
        {
            count=0;
            while(str[i]>='0' && str[i]<='9')
            {
                count++;
                i++;
            }
            if(count>=max)
            {
                if(count>max) k=0; //如果新的最大值比之前的最大值大,把输出子串清空
                max=count>max ? count : max;
                for(j=0;j<max;j++) //如果新的最大值等于之前的最大值,输出子串不清空
                {
                    num[k]=str[i-count+j];
                    k++;
                }
                num[k]='\0';       //输出子串最后加个空字符
            }
            i++;
        }
        printf("%s,%d\n",num,max);
    }
    return 0;
}

发表于 2023-11-29 02:21:28 回复(0)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define N 100

int main()
{
    char s[N] = {0};
    while(scanf("%s", s) != EOF){
        int len = strlen(s);
        int maxLen = 0;
        char res[N] = {0};
        char temp[N] = {0};
        int cnt = 0, tempCnt = 0;
        s[len++] = 'a';
        for(int i = 0; i < len; i++){
            if(s[i] >= '0' && s[i] <= '9'){
                temp[tempCnt++] = s[i];
            }else{
                if(tempCnt > maxLen){
                    maxLen = tempCnt;
                    cnt = 0;
                    for(int j = 0; j < tempCnt; j++){
                        res[cnt++] = temp[j];
                    }
                    res[cnt++] = '\0';
                    tempCnt = 0;
                }else if(tempCnt == maxLen && tempCnt > 0){
                    cnt--;
                    for(int j = 0; j < tempCnt; j++){
                        res[cnt++] = temp[j];
                    }
                    res[cnt++] = '\0';
                    tempCnt = 0;	
                }else{
                   tempCnt = 0; 
                }
            }
        }
        printf("%s,%d\n", res, maxLen);
    }
    return 0;
}

发表于 2023-11-05 17:51:54 回复(0)
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char str[201] = { '\0' };
    while (scanf("%s", str) != EOF) {
        int num = 0;
        char ret[201] = { '\0' };
        int flag = 0;
        int tmp = 0;
        int len = strlen(str);
        char mem[201] = { '\0' };
        for (int i = 0; i < strlen(str); i++)
        {
            if (isdigit(str[i]))
            {
                if (flag == 0)
                {
                    flag = 1;
                    tmp = 0;
                    memset(mem, '\0', 201);
                }
                mem[tmp] = str[i];
                tmp++;
            }
            else
            {
                if (flag)
                {
                    flag = 0;
                    if (tmp > num)
                    {
                        num = tmp;
                        memset(ret, '\0', 201);
                        strcpy(ret, mem);
                    }
                    else if (tmp == num)
                    {
                        strcat(ret, mem);
                    }
                }
            }
        }
        if (flag)
        {
            flag = 0;
            if (tmp > num)
            {
                num = tmp;
                memset(ret, '\0', 201);
                strcpy(ret, mem);
            }
            else if (tmp == num)
            {
                strcat(ret, mem);
            }
        }
        printf("%s,%d\n", ret, num);
    }
    return 0;
}
发表于 2023-10-23 13:43:40 回复(0)

一遍while找最长值,一边while打印

#include 
#include 
int main()
{
    char str[201] = {0}, *beg, *end;
    int max = 0;

    while (scanf("%s", str) != EOF) {
        beg = end = str;
        max = 0;
        while (*end) {
            if (!isdigit(*end)) {
                if (max < (end - beg))
                    max = end - beg;
                beg = end + 1;
            }
            end++;
        }
        if (max < (end - beg))
            max = end - beg;
        beg = end = str;
        while (*end) {
            if (!isdigit(*end)) {
                if (max == (end - beg)) {
                    *end = 0;
                    printf("%s", beg);
                }
                beg = end + 1;
            }
            end++;
        }
        if (max == (end - beg))
            printf("%s", beg);
        printf(",%d\n", max);
    }
    return 0;
}
发表于 2022-06-20 15:07:19 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
int main() {
    char s[200];
    while ( scanf("%s", s) != EOF) {
        int i, j, k, max = 0;
        int len = strlen(s);
        int map[len+1];
        memset(map, 0, sizeof(int) * (len + 1));
        for (i = len - 1; i >= 0; --i) {
            if (s[i] >= '0' && s[i] <= '9') {
                if (i == len - 1) {
                    map[i] = 1;
                } else {
                    map[i] = map[i + 1] + 1;
                }
            } else {
                map[i] = 0;
            }
            max = max > map[i] ? max : map[i];
        }
        for (i = 0; i < len - 1; ++i) {
            if (map[i] == max) {
                for ( k = 0 ; k < max; ++k, ++i) {
                    printf("%c", s[i]);
                    fflush(stdout);
                }
            }
        }
        /*        for (i = 0; i < len - 1; ++i) {
                    if (map[i] == max) {
                        if(i+max<len-1){
                            s[i + max]  = '\0' ;
                            printf("%s", s + i);
                            s[i + max]  = 'o';
                        }
                        else{
                            printf("%s", s + i);
                        }
                    }
                }*/
        printf(",%d\n", max);

    }


    return 0;
}
发表于 2022-06-19 22:38:47 回复(0)
#include "stdio.h"
#include "string.h"
#include "ctype.h"

int main()
{
    char a[201] = {0};
    
    while (scanf("%s", a) != EOF) {
        int n, i, j;
        n = strlen(a);
        
        char str[200] = {0};
        char b[200] = {0};
        int k = 0;
        int len = 0;

        for (i = 0; i < n; i++) {
            j = i;
            k = 0;
            memset(b, 0, sizeof(b));
            while (isdigit(a[j])) {
                b[k++] = a[j];
                j++;
                if (j >= n) {
                    break;
                }
            }
            if (k > len) {
                len = k;
                strcpy(str, b);
            } else if (k == len) {
                strncat(str, b, k);
            }
        }
        printf("%s,%d\n", str, len);
    }
    return 0;
}
发表于 2022-05-17 19:59:58 回复(0)
#include <stdio.h>
#define    N    200
int main()
{
    char str[N];
    int pos[N],i=0,j,cnt,len,flag,max_len,start;
    while(gets(str))
    {
        len=0;flag=0;max_len=0;cnt=0;
        while(str[i]!='\0')
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                if(flag==0)
                {
                    start=i;
                    flag=1;
                }
                len++;
            }
            else
            {
                if(flag)
                {
                    if(len>max_len)
                    {
                        cnt=0;
                        max_len=len;
                        pos[cnt++]=start;
                    }
                    else if(len==max_len)
                    {
                        pos[cnt++]=start;
                    }
                    flag=0;
                    len=0;
                }
            }
            i++;
        }
        if(flag)
        {
            if(len>max_len)
            {
                cnt=0;
                max_len=len;
                pos[cnt++]=start;
            }
            else if(len==max_len)
            {
                pos[cnt++]=start;
            }
        }
        for(i=0;i<cnt;i++)
        {
            for(j=pos[i];j<pos[i]+max_len;j++)
            {
                printf("%c",str[j]);
            }
        }
        printf(",%d\n",max_len);
    }
    return 0;
}

发表于 2022-05-08 09:31:33 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

int main()
{
	char a[250] = { 0 };
	int x[250] = { 0 };
	int s, i, j,k=0,sum=0;

	while (gets(a) != NULL)
	{
		k = 0;
		int flag = 0;
		s = strlen(a);
		sum = 0;
		for (i = 0; i < s; i++)
		{
			if (a[i] >= '0' && a[i] <= '9')
			{
				if (flag == 0)
				{
					k = i;
					flag = 1;
				}
				x[k]++;

			}
			else
			{
				flag = 0;
			}
		}
		int max = 0;
		for (i = 0; i < 250; i++)
		{
			if (x[i] > max)
				max = x[i];
		}
		for (i = 0; i < 250; i++)
		{
			if (x[i] == max)
			{
				for (j = i; j < i + max; j++)
					printf("%c", a[j]);
			}
		}
		printf(",%d\n", max);

		memset(a, 0, sizeof(a));
		memset(x, 0, sizeof(x));
	}




	return 0;
}

发表于 2022-02-10 11:33:12 回复(0)
还以为我的应该比较快,实际上击败24%不到。。
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char u8;

u8 isnum(char c)
{
    return (c>'9'||c<'0')?0:1;
}

u8 getnumlen(char* str)
{
    u8 i;
    for(i=0;;i++)
        if(!isnum(str[i])) 
            return i;
    return i-1;
}

#define RESET_ALL \
memset(str,0,sizeof(str));\
memset(len,0,sizeof(len));\
memset(max_index,0,sizeof(max_index));\
max = 0;\
i = 0;\
j = 0;\
k = 0;

#define RESET_J \
memset(max_index,0,sizeof(max_index));\
j=0;

int main()
{
    char str[201];
    u8 len[201];
    u8 max_index[101];
    u8 max;
    int i,j,k;
    
    RESET_ALL;
    while(EOF!=scanf("%s",str))
    {
        for(i=0;str[i]!=0;)
        {
            if(isnum(str[i]))
            {
                len[i]=getnumlen(&str[i]);
                if(max<len[i])
                {
                    RESET_J;
                    max = len[i];
                    max_index[j] = i;
                    j++;
                }
                else if(max==len[i])
                {
                    max_index[j] = i;
                    j++;
                }
                
                i+=len[i];
            }
            else i++;
        }
        for(i=0;i<j;i++)
            for(k=0;k<max;k++)
                printf("%c",str[max_index[i]+k]);
        printf(",%d\n",max);
        RESET_ALL;
    }
    return 0;
}


发表于 2022-01-10 20:24:06 回复(0)
// 使用字符串数组qsort排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *g_array[100];
int g_index = 0;

int GetIndex(char *s)
{
    for (int i = 0; i < 100; i++) {
        if (g_array[i] != NULL && strcmp(s, g_array[i]) == 0) {
            return i;
        }
    }
    
    return -1;
}

void InsertArray(char *s)
{
    int i = GetIndex(s);
    // 字符串已存在不再插入
    if (i >= 0) {
        return;
    }
    g_array[g_index] = s;
    g_index++;
}

void FreeArray(void)
{
    for (int i = 0; i < g_index; i++) {
        if (g_array[i] != NULL) {
            free(g_array[i]);
            g_array[i] = NULL;
        }
    }
}

int Cmp(const void *p1, const void *p2)
{
    char *a = *(char **)p1;
    char *b = *(char **)p2;
    
    // 按字符串长度从小到大排序
    return strlen(b) - strlen(a);
}

int main(void)
{
    char str[1000] = {'\0'};
    
    while (gets(str)) {
        // 多组数组需要在每次循环初始化全局变量
        memset(g_array, 0, sizeof(g_array));
        g_index = 0;
        int i = 0;
        while (i < strlen(str)) {
            if (str[i] >= '0' && str[i] <= '9') {
                int len = 0;
                int startIndex = i;
                while (i < strlen(str)) {
                    // 计算数字长度
                     if (str[i] >= '0' && str[i] <= '9') {
                         len++;
                         i++;
                         // 处理字符末尾为数字情况
                         if (str[i] == '\0') {
                             char *tmp = (char *)malloc(sizeof(char) * (len + 1));
                             strncpy(tmp, &str[startIndex], len);
                             tmp[len] = '\0';
                             InsertArray(tmp);
                         }
                     } else {
                         // 复制纯数字字符串到字符串数组
                         char *tmp = (char *)malloc(sizeof(char) * (len + 1));
                         strncpy(tmp, &str[startIndex], len);
                         tmp[len] = '\0';
                         InsertArray(tmp);
                         break;
                     }
                }
            } else {
                i++;
            }
        }
        qsort(g_array, g_index, sizeof(char *), Cmp);
        for (int i = 0; i < g_index; i++) {
            // 处理字符串长度相等情况
            if (strlen(g_array[i]) == strlen(g_array[0])) {
                printf("%s", g_array[i]);
            }
        }
        printf(",%d\n", strlen(g_array[0]));
        FreeArray();
    }
}

发表于 2021-09-20 17:24:59 回复(0)

问题信息

难度:
14条回答 69527浏览

热门推荐

通过挑战的用户

查看代码