输入一个长度为
、由数字和小写字母混合构成的字符串
。保证至少存在一个数字子串。
记最长的数字子串长度为
,有
个长度为
的数字子串。在一行上先首尾相连的输出
个长度为
的数字子串(不使用空格分割),随后输出一个逗号,再输出
。
abcd12345ed125ss123058789
123058789,9
11a22b33c
112233,2
在这个样例中,数字子串
长度均为
,都是最长的数字子串。
本题数据已规范为单组询问(2025/01/15)。
line = input()
# 除数字外其余字符变为空格
for l in line:
if not l.isdigit():
line = line.replace(l, " ")
# 以空格为分隔符 + 去除none元素
temp_str = [l for l in line.split(" ") if l != ""]
# 标记最大长度
count = len(max(temp_str, key=len))
# 拼接相同长度的最长字符串
print("".join([l for l in temp_str if len(l) == count]) + "," + str(count))
s = input()
l = []
l_index = []
for i in range(len(s)):
if s[i].isdigit() is False:
s = s.replace(s[i],' ')
s = list(s.split(' '))
len_all = []
for i in s:
len_all.append(len(i))
n = max(len_all)
for i in s:
if len(i) == n:
print(i,end='')
print(',',end='')
print(n) while True:
try:
s = input()
temp_long = 0
max_long = 0
c = []
d = ''
# 求最长子串的长度
for i in range(len(s)):
if s[i].isdigit():
temp_long+=1
else:
c.append(temp_long)
temp_long = 0
c.append(temp_long)
max_long = max(c)
# 求最长子串
temp_long = 0
for i in range(len(s)):
if s[i].isdigit():
temp_long += 1
if temp_long == max_long:
d += s[i-max_long+1:i+1]
else:
temp_long = 0
print(f"{d},{max_long}")
except:
break
有点麻烦了
def func(s:str):
res = []
n = 0 # 记录最长子串的长度值
longer_detect = False # 检测到更长的子串时,开启开关
for i in range(len(s)):
if s[i-n:i+1].isdigit(): # 下标i+1控制待检测子串总比最大长度值n加了1格
res = [] # 有更长的子串时,之前res记录的子串都清空
n += 1
longer_detect = True
elif longer_detect: # 检测最长数字子串后遇到字母,把刚才的子串加入res
res.append(s[i-n:i])
longer_detect = False
elif s[i-n:i].isdigit(): # 以上只将更长的子串加入res,这里处理子串长度和n相同
res.append(s[i-n:i])
if s[-n:].isdigit(): # 若末尾最后还有一个n长度纯数字子串,加入res
res.append(s[-n:])
print(''.join(res)+','+str(n))
while True:
try:
s = input()
func(s)
except:
break
def length(s: str):
num_len = 0
num_key = ''
num_dict = {}
for i in range(len(s)):
if s[i].isdigit():
num_len += 1
num_key = num_key + s[i]
else:
if num_len >= 1:
try:
num_dict[num_len] += num_key
except:
num_dict[num_len] = ''
num_dict[num_len] += num_key
num_len = 0
num_key = ''
if (i == len(s)-1) and s[i].isdigit():
try:
num_dict[num_len] += num_key
except:
num_dict[num_len] = ''
num_dict[num_len] += num_key
return num_dict
while True:
try:
def_dict = length(input())
max_len = max(def_dict.keys())
print(def_dict[max_len] + ',' + str(max_len))
except:
break for line in sys.stdin:
a = line.strip()
b=[str(i) for i in range(10)]
l=['']
max=0
for i in a:
if i not in b and l[-1]!="":
if len(l[-1])>max:
max=len(l[-1])
l.append("")
elif i in b:
l[-1]+=i
if len(l[-1])>max:
max=len(l[-1])
s=''
for i in l:
if len(i)==max:
s+=i
print(s+','+str(max)) def fun(str):
dic = {}
lef = 0
right = 0
while lef < len(s):
if s[lef].isdigit() == True:
right = lef + 1
while s[right].isdigit():
right += 1
# a = s[right] 用来看right到哪里了,后续超过下标一直报错
# 如果把这行代码放在上面就没有这个风险了,因为判断语句能使用
if right < len(s):
continue
else:
break
str = s[lef:right]
dic[str] = len(str)
lef = right
else:
lef += 1
right = lef
max = sorted(dic.values())[-1]
max_key = ""
for key, val in dic.items():
if val == max:
max_key = max_key + key
print(max_key, end=",")
print(val)
while True:
try:
s = input()
fun(s)
except:
break
双指针
while True:
try:
s = input()
# s = "abcd12345ed125ss123058789"
n = len(s)
ans = []
left = 0
right = 0
flag = 0
max_len = 0
while right < n:
while right<n and not s[right].isnumeric():
right += 1
flag = 1
left = right
while right< n and s[right].isnumeric():
right += 1
if right - left >= max_len:
ans.append((left, right))
max_len = right - left
flag = 0
left = right
if flag:
ans.append((left, n))
for left,right in ans:
if right-left==max_len:
print(s[left : right],end="")
print(f",{max_len}")
except:
break
while True:
try:
ret = []
s = input()
for i in range(len(s) + 1):
for j in range(i):
_s = s[j:i]
if _s.isdigit():
ret.append(_s)
max_num = max([len(i) for i in ret])
new_s = "".join(list(map(lambda x: x if len(x) == max_num else "", ret)))
print(f"{new_s},{max_num}")
except Exception as e:
break
import sys
while True:
try:
ss = input()
dic = {}
max_len = 0
left, right = 0, 0
while right < len(ss):
if not ss[left].isdigit():
left += 1
right = left
else:
while right < len(ss) and ss[right].isdigit():
right += 1
sub = ss[left:right]
length = right - left
if length not in dic:
dic[length] = sub
else:
dic[length] += sub
max_len = max(length, max_len)
left = right
if max_len != 0:
print(str(dic[max_len])+','+str(max_len))
except:
break