输入一个长度为
、由数字和小写字母混合构成的字符串
。保证至少存在一个数字子串。
记最长的数字子串长度为
,有
个长度为
的数字子串。在一行上先首尾相连的输出
个长度为
的数字子串(不使用空格分割),随后输出一个逗号,再输出
。
abcd12345ed125ss123058789
123058789,9
11a22b33c
112233,2
在这个样例中,数字子串
长度均为
,都是最长的数字子串。
本题数据已规范为单组询问(2025/01/15)。
双指针
s = input()
n, arr, mm = len(s), [], 0
i, j = 0, 0
while j < n:
if s[j].isdigit():
while j < n and s[j].isdigit():
j += 1
mm = max(j-i, mm)
arr.append(s[i:j])
i, j = j + 1, j + 1
newArr = [i for i in arr if len(i) == mm]
print(f"{''.join(newArr)},{mm}") 正则表达式
import re
s = input()
arr = re.findall(r'\d+', s)
maxLen = max(len(x) for x in arr)
newArr = [c for c in arr if len(c) == maxLen]
print(f"{''.join(newArr)},{maxLen}")
while True: try: s = input().strip() for ch in s: if ch.isalpha(): s = s.replace(ch,' ') num = list(map(str,s.strip().split())) res = [] n = len(num) ans = '' for i in range(n): res.append(len(num[i])) m = max(res) for i in range(n): if len(num[i]) == m: ans += num[i] print(ans,m,sep=',') except: break
import re
ls = []
while True:
try:
ls.append(input())
except:
for s in ls:
temp = re.findall('\d+',s)
lenth = max(len(x) for x in temp)
res = ''
for i in temp:
if len(i)==lenth:
res += i
print(res,lenth,sep=',')
break while True:
try:
a = input()
vis = [0]*len(a)
for i,j in enumerate(a):
if j.isdigit():
vis[i] = vis[i-1] + 1
else:
vis[i] = 0
Lmax = max(vis)
for n,m in enumerate(vis):
if m == Lmax:
print(a[n-Lmax+1:n+1],end='')
print(",{}".format(Lmax))
except:
break import re
while True:
try:
text = input()
t = re.findall(r'\d+',text)
t2 = max([len(i) for i in t])
for i in t:
if len(i) == t2:
print(i,end='')
print('',end=',')
print(t2)
except:
break 来个正则的import re
while True:
try:
s = input()
res = re.findall('\d*', s)
d = dict()
for ch in res:
d[ch] = len(ch)
result_max =max(d.values())
l=[]
for ind, val in d.items():
if val == result_max:
l.append(ind)
print(''.join(map(str,l))+ ',' + str(result_max))
except:
break