首页 > 携程9.8算法 ak 分享题解
头像
CodeSausage
编辑于 2020-09-09 10:40
+ 关注

携程9.8算法 ak 分享题解 投票

不知道携程hc还多不多,反正题目真的不难,连我等渣渣都可以出来写题解了……orz
反观隔壁投的携程开发的同学,根据大家的讨论,好像题目出的还是比较难的😅😅

  1. 题目意思大概就是,两个字符串S1和S2,找出其中最长的子串X,该子串可以通过重复拼接自己的方式,得到S1和S2.
  • 代码如下:
    s1 = input()
    s2 = input()
    find_flag = False
    
    n1 = len(s1)
    n2 = len(s2)
    
    if n1 < n2:
      s1, s2 = s2, s1
      n1, n2 = n2, n1
    
    for i in range(n2, 0, -1):
      if n1 % i == 0 and n2 % i == 0:
          if s2[:i] * (n1 // i) == s1 and s2[:i] * (n2 // i) == s2:
              print(s2[:i])
              find_flag = True
              break
    
    if find_flag == False:
      tmp = ""
      print(tmp)
  1. 题目意思,就是斜着输出矩阵。
  • 代码如下:

    r, w = map(int, input().split())
    res = [ [0 for i in range(w)] for j in range(r) ]
    cnt = 1
    
    for i in range(r+w-1):
      row, col = i, 0
      while(col <= (w-1) and row >= 0):
          if row < r and col >=0:
              res[row][col] = cnt 
              cnt += 1
    
          row -= 1
          col += 1 
    print(res)
  1. 题目的意思是,对字符串进行编码,并且题目指定了编码规则。具体规则大概意思就是a对应1,b对应2,……,z对应26,A对应27,B对应28,……,Z对应52,0对应53,……,9对应62(要把这里的对应结果转为二进制)。
  • 代码如下:

    s = input()
    nums  = list(s)
    
    string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    dic = {}
    cnt = 1
    
    for i in string:
      dic[i] = cnt 
      cnt +=1
    
    res = []
    tmp = "00"
    
    for i in range(len(nums)):
      if i % 5 == 0 and i!= 0:
          res.append(tmp)
          tmp = "00"
    
      if nums[i] not in string:
          tmp += '000000'
          continue
      else:
          tmp_ord = ""
          nn = dic[nums[i]]
          for _ in range(6):
              if nn == 0:
                  tmp_ord += '0'
              else:
                  tmp_ord += str(nn%2)
                  nn = nn // 2
    
          tmp_ord = tmp_ord[::-1]
          tmp += tmp_ord
    
    res.append(tmp)
    res_1 = []
    
    for i in res:
      res_1.append(int(i,2))  print( " ".join([str(i) for i in res_1]))

我的思路不一定是最优的,欢迎大家交流。

最后加个投票


全部评论

(4) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐