Найти подпоследовательность максимальной длины, которая имеет арифметический смысл и содержит только цифры
Возле цифры 0 не может быть знака операции.
В выражении не должно быть умножения на отрицательные числа.
В выражении не должно быть повторяющихся знаков операций, таких как «**».
Пример правильного выражения: 6787-86.
Пример неправильного выражения: 6786*−78 (содержит повторяющиеся знаки операции и отрицательное число).
В ответе запишите длину найденной последовательности.
Приведём решение на языке Python.
a = open('69932.txt').readline()
nedopustim_posled = [('--','- -'), ('**','* *'),('-0', '- '), ('*0','* '), ('0-',' -'), ('0*',' *'), ('-*','- *'), ('*-','* -')]
for simbol in nedopustim_posled:
while simbol[0] in a:
a = a.replace(simbol[0], simbol[1])
a = a.split()
maxi = -1
for i in a:
if len(i) > 0 and i[-1] in '* ':
i = i[:-1]
if len(i) > 0 and i[0] in '*':
i = i[1:]
if len(i) > 0 and i[-1] in '-':
i = i[:-1]
if len(i) > 0 and i[0] in '-':
i = i[1:]
while len(i) > 0 and i[0] =='0':
i = i[1:]
if '*' in i or '-' in i:
maxi = max(maxi,len(i))
print(maxi)
Ответ: 40.
Приведём решение Юрия Лысакова на языке Python.
f = open('69932.txt')
s = f.read()
k,max1 = 0,0
for i in range(len(s)-1):
if (s[i] in '6780') or (s[i-1] in '678'and s[i+1] in '678' and k != 0):
k += 1
else:
max1 = max(max1,k)
k = 0
print(max1)
Приведём решение Евгения Романова на языке Python.
s = open('69932.txt').readline().replace('**', '* *').replace('**', '* *').replace('--', '- -').replace('--','- -').replace('-*', '- *').replace('*-', '* -').split()
a = [x.strip('-').strip('*').rstrip('*').rstrip('-') for x in s if all(y not in x for y in ('-0', '0-', '*0', '0*'))]
m=max(a, key=len)
print(len(m))
Приведём решение Юрия Ворошилова на языке Python.
t = open('69932.txt').read()
t=t.replace('-','*')
while '*0*' in t:
t=t.replace('*0*','*5*')
t=t.replace('*0','x')
while 'x0' in t:
t=t.replace('x0','x')
t=t.replace('0*','x')
while '0x' in t:
t=t.replace('0x','x')
while '**' in t:
t=t.replace('**','x')
a=t.split('x')
ml=0
for i in a:
if len(i)>1:
if i[0]=='*':
i=i[1:]
if i[-1]=='*':
i=i[:-1]
ml=max(len(i),ml)
print(ml)
Приведём решение Бориса Савельева на языке Python.
f = open('69932.txt').readline()
f = f.replace('-0','!')
f = f.replace('0-','!')
f = f.replace('0*','!')
f = f.replace('*0','!')
f = f.replace('*-','!')
f = f.replace('**','!')
f = f.replace('-*','!')
f = f.replace('--','!')
f = f.replace('-!','!')
f = f.replace('!-','!')
f = f.replace('!*','!')
f = f.replace('*!','!')
f = f.split('!')
maxi=0
for i in range (0,len(f)):
s=f[i]
cnt=0
flag=0
for j in range (0,len(s)):
if flag==0 and f[j]!='0':
flag=1
if flag==1:
cnt+=1
maxi=max(maxi,cnt)
print(maxi)

