2 aishangjianguo aishangjianguo 于 2015.06.27 22:46 提问

python字符串的处理问题

给定一个字符串找出不同位置重复出现且长度最长的的第一个子字符串,输出子字符串以及其首字符的位置,若无任何重复子串,返回-1

2个回答

devmiao
devmiao   Ds   Rxr 2015.06.27 22:51
yuyuhupo
yuyuhupo   2015.06.28 10:39

ss = 'asd111111sda222wwwwwwqwqqaaassssdddd'
index = len(ss) - 1
ss_dict = {}
ss_n = {}
value_l = []
def cale_char(strings):
count = 0
while count < index:
c = count
while True:
if count < index and strings[count] == strings[count+1]:
count = count + 1
else:
break
if count > c and strings[c] != ' ':
ss_r = strings[c]*(count-c+1)
ss_dict[c] = ss_r
ss_n[c] = str(count-c+1)
count = count + 1
for key,value in ss_n.items():
value_l.append(value)
max_value = max(value_l)
for key,value in ss_n.items():
if int(value) == int(max_value):
print('Strings:%s\tIndex:%s\t\tLength:%s\n' % (ss_dict[key],key,value))
if len(ss_dict) == 0:
return -1
else:
return 0

print(cale_char(ss))

 我也是新手,刚接触python,一起学习。
Csdn user default icon
上传中...
上传图片
插入图片