2018年9月7号

ali算法笔试编程题

a = raw_input()

b = raw_input()

#a = "singer_周杰|周杰伦|刘德华|王力宏;song_冰雨|北京欢迎你|七里香;actor_周杰伦|孙俪"
#b = "请播放周杰伦的七里香给我听"

alist = []
i = 0
j = 0
while i  < len(a):
    if a[i] == ';':
        alist.append(a[j:i])
        j = i+1
    i = i + 1
alist.append(a[j:])
alist[0] = alist[0].replace('singer_','')
alist[1] = alist[1].replace('song_','')
alist[2] = alist[2].replace('actor_','')
#print(alist)

aalist=[]
for c in alist:
    #print(c)
    ssa = []
    i = j = 0
    while i < len(c):
        if c[i] == '|':
            ssa.append(c[j:i])
            j = i + 1
        i = i + 1
    ssa.append(c[j:])
    aalist.append(ssa)
print(aalist)
#myList1 = sorted(myList,key = lambda i:len(i),reverse=True)
aalist[0] = sorted(aalist[0],key=lambda i:len(i),reverse=True)
#print(aalist)
i = 0
#count = 0
while i <len(b):

    count = 0
    for j in aalist[0]:
        #print(j)

        if b[i] == j[0]:
            #print('b[i]'+b[i])


            if b[i:i+len(j)] == j:
                 if j in aalist[2]:
                    b = b.replace(j, ' ' +j + '/acter,singer ')
                    i = i + len(j)
                 else:
                    b = b.replace(j,' ' +j+'/singer ')
                    i = i + len(j) - 1

    for j in aalist[1]:
        if b[i] == j[0]:
            if b[i:i+len(j)] == j:
                b = b.replace(j,' ' +j+'/song ')
                i = i + len(j)-1
    i = i+1

print(b)

 

输出为:

请播放 周杰伦/acter,singer 的 七里香/song 给我听

 

 

相关文章:

  • 2021-06-09
  • 2021-05-15
  • 2021-05-22
  • 2021-08-25
  • 2021-07-30
  • 2021-12-02
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2021-09-06
  • 2021-09-27
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案