版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huanhuanq1209/article/details/82793496
第一题:
简化Unix风格的路径:
实例:
输入:/home/
输出:/home
实例2:
输入:/a/./b/../../c/
输出:/c
def test(path):
pathlist=path.split('/')
catch=[]
result=''
for data in pathlist:
if data not in ["",".",".."]:
catch.append(data)
if '..'==data and catch:
catch.pop(-1)
if catch==[]:
return "/"
for data in catch:
result=result+"/"+data+""
return result
if __name__=='__main__':
path=raw_input('')
result=test(path)
print result
运行结果:
运行AC
第二题:
得出字符串前缀,来唯一标识该字符串;
if __name__=='__main__':
n=int(raw_input())
strlist=[]
result=[]
t=[]
flag = 0
for i in range(n):
catch=raw_input()
strlist.append(catch)
for i in range(0,len(strlist)):
result=[]
for j in range(0,len(strlist)):
for y in range(0,len(strlist[i])+1):
if i!=j:
item=strlist[i][:y]
if strlist[j].find(item)!=0 :
result.append(strlist[i][:y])
result.sort()
break
print result.pop()
# #
# 5
# bytedance
# toutiaohao
# toutiaoapp
# iesaweme
# iestiktok
# b
# toutiaoh
# toutiaoa
# iesa
# iest
# #
运行结果为:
结果:
运行20%;运行时间超时。。。。。。。。
欢迎大佬指点!!!!!!!!!!!!!