输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)
本题含有多组样例输入。
数据范围:字符串长度 , 保证每组输入都至少含有一个数字
输入一个字符串。1<=len(字符串)<=200
输出字符串中最长的数字字符串和它的长度,中间用逗号间隔。如果有相同长度的串,则要一块儿输出(中间不要输出空格)。
abcd12345ed125ss123058789 a8a72a6a5yy98y65ee1r2
123058789,9 729865,2
样例一最长的数字子串为123058789,长度为9 样例二最长的数字子串有72,98,65,长度都为2
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
while True: try: s = input() s1 = '' for i in s: if i.isalpha(): s1 += ' ' else: s1 += i l = list(s1.split()) len_l = [] for i in l: len_l.append(len(i)) for i in l: if len(i) == max(len_l): print(i,end='') print(',',end='') print(max(len_l)) except: break
number = "0123456789" for line in sys.stdin: a= line.split("\n")[0] i,j = 0,0 max_length = 0 temp = [] while i <len(a) and j < len(a): if a[i] in number: j = i while j < len(a) and a[j] in number: j += 1 max_length = max(max_length,j-i) temp.append(a[i:j]) i = j else: i += 1 res = '' for i in temp: if len(i) == max_length: res += i print(str(res) + "," + str(max_length))
#不用re while True: try: s=input() for i in s: if not i.isdigit(): s=s.replace(i,' ') s=s.split(' ') l=0 ss='' for i in s: if len(i)>l: l=len(i) for i in s: if len(i)==l: ss+=i print(ss,end=',') print(l) except: break #用re import re while True: try: s=input() pattern=re.compile(r'\d+') s=pattern.findall(s) s=sorted(s,key=lambda x:len(x),reverse=True) l=len(s[0]) ss='' for i in s: if len(i)>=l: ss+=i print(ss+','+str(l)) except: break
import sys def isnum(char): if char >= '0' and char <= '9': return True return False def numstring(s): l =len(s) ns = [] nums = '' lgest = 1 for i in range(l-1): if not isnum(s[i]) and isnum(s[i+1]): s = s.replace(s[i], '#') elif isnum(s[i]) and not isnum(s[i+1]): s = s.replace(s[i+1], '#') ns = s.split('#') for string in ns: if len(string) == 0: continue if isnum(string[0]): lgest = max(lgest, len(string)) for string in ns: if len(string) == lgest and isnum(string[0]): print(string,end='') print(','+str(lgest)) for line in sys.stdin: a = line.split() numstring(a[0])