描述:
给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2]。 假定数列中无重复元素,且数列长度为奇数。 求出旋转数列的中间值。如数列[4,5,6,7,0,1,2]的中间值为4。
输入:
4,5,6,7,0,1,2
输出:
4
输入样例:
1 1,2,3 4,5,6,7,0,1,2 12,13,14,5,6,7,8,9,10
输出样例:
1 2 4 9
思路:
多组输入,每个序列用“,”隔开,将每组输入的序列中的每个数列分别复制到三个序列(s1,s2,s3)中。(注意除去序列s3中‘/n’)并计算每个序列的长度。从s1第一个字母开始匹配s3中字母,如果匹配则计数h1++,不匹配则让s2开始匹配,如果匹配h2++,最后进行结果判断。
代码:
import sys
for line in sys.stdin:
# line = line.split(',')
# s1,s2,s3=list(line[0]),list(line[1]),list(line[2])
s1,s2,s3=line.strip().split(',') #strip()除去字符串头尾的指定字符
l1,l2,l3=len(s1),len(s2),len(s3)
print(s3)
h1=h2=h3=0
while h3<l3:
t3=s3[h3]
if h1<l1 and t3==s1[h1]:
h1+=1
if h2<l2 and t3==s2[h2]:
h2+=1
h3+=1
print(h1,h2,h3)
print(l1,l2,l3)
if h1 == l1 and h2 == l2 and h3 == l3 and l3 == (l1+l2):
print('true')
else:
print('false')