1.
字符串逆序
import sys
'''
waimai,dache,lvyou,liren,meishi,jiehun,lvyoujingdian,jiaopei,menpiao,jiudian
'''
s=sys.stdin.readline().strip().split(",")
if len(s)==0:
print ""
elif len(s)==1:
print s[0]
else:
res=[]
for i in range(len(s)-1):
for j in range(i+1,len(s)):
if s[i] in s[j] :
continue
elif s[j] in s[i]:
tmp = s[i]
s[i] = s[j]
s[j] = tmp
continue
if s[i]<s[j]:
tmp=s[i]
s[i]=s[j]
s[j]=tmp
for i in s:
sys.stdout.write(i)
sys.stdout.write(",")
通过17%
2.最长公共前缀,AC了。
import sys
n=int(sys.stdin.readline().strip())
ss=[]
for i in range(n):
ss.append(sys.stdin.readline().strip())
while True:
a,b=map(int,sys.stdin.readline().strip().split(" "))
a=a-1
b=b-1
if ss[a]==[] or ss[b]==[]:
print 0
break
count=0
i,j=0,0
while i<len(ss[a]) and j<len(ss[b]):
if ss[a][i]==ss[b][j]:
count+=1
i+=1
j+=1
else:
break
print count
答案版权属实验室室友,室友