首页 > 试题广场 >

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

[编程题]在字符串中找出连续最长的数字串
  • 热度指数:146857 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的由数字和小写字母混合构成的字符串 s,找到其中最长的数字子串。如果由多个相同长度的数字子串,则需要全部输出,具体输出的格式请参考输出描述。

\hspace{15pt}子串为从原字符串中,连续的选择一段字符(可以全选、可以不选)得到的新字符串。

输入描述:
\hspace{15pt}输入一个长度为 1 \leqq {\rm len}(s) \leqq 200、由数字和小写字母混合构成的字符串 s。保证至少存在一个数字子串。


输出描述:
\hspace{15pt}记最长的数字子串长度为 l,有 m 个长度为 l 的数字子串。在一行上先首尾相连的输出 m 个长度为 l 的数字子串(不使用空格分割),随后输出一个逗号,再输出 l
示例1

输入

abcd12345ed125ss123058789

输出

123058789,9
示例2

输入

11a22b33c

输出

112233,2

说明

\hspace{15pt}在这个样例中,数字子串 \texttt{ 长度均为 2,都是最长的数字子串。

备注:
\hspace{15pt}本题数据已规范为单组询问(2025/01/15)。
头像 🐼201908171342330
发表于 2021-06-27 11:11:21
思路 把非数字的全部替换成空格,然后切割; 遍历一次得到最大长度; 再遍历一次,把符合上述最大长度的字符串取出作追加拼接。 代码 while True: try: s = input() for c in s: if not c.i 展开全文
头像 恒成立
发表于 2021-03-28 17:25:10
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception{ Scanner sc = new Scanne 展开全文
头像 牛客875694424号
发表于 2021-12-07 22:00:35
dp解,简单直接 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); 展开全文
头像 涛哥比较无聊
发表于 2021-10-23 17:38:23
import re while True: try: string = input() lists = re.findall('\d+', string) lens = max([int(len(x)) for x in lists]) 展开全文
头像 昵称已被占用啦啦啦
发表于 2020-10-03 19:57:58
while True: try: s=input() for i in s: if i.isdigit()==False: s=s.replace(i," ") s=s.split() r1=max([len(i) fo 展开全文
头像 抗英主力法兰西
发表于 2021-03-24 19:14:59
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> using namespace std; int main(){ 展开全文
头像 牛客313925129号
发表于 2021-11-27 18:07:20
题意理解 在字符串中找到长度最大的数字子串,如果有多个数字子串长度相等且最大,则按其在字符串中的顺序一次输出。数字子串指由数字连续构成的子串。 方法一 模拟,双指针。从左到右扫描字符串,并重复一下操作: 1、遇到一个数字字符,位置为i,暂停; 2、从i开始往后扫描,直到遇到非数字字符,位置为j; 3 展开全文
头像 想吃火锅的猪猪是我的神
发表于 2022-03-18 11:29:32
#include<stdio.h> #include<string.h> #define N 200 int main(){ char str[N]; while(scanf("%s",str)!=EOF){ int max=0,count=0 展开全文
头像 你敲代码的样子好像蔡徐坤
发表于 2021-10-04 17:39:13
#include<bits/stdc++.h> using namespace std; int main() { string str; //存放输入的字符串 while(getline(cin, str)) { //获取输入的一行字符串 v 展开全文
头像 大数据内推达人
发表于 2022-05-07 20:51:46
清晰!遍历一遍获取长度最大值+正则匹配判断,时间复杂度O(n) import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Sc 展开全文