123
返回列表 发新帖
楼主: qiweiwang89
跳转到指定楼层
上一主题 下一主题
收起左侧

新鲜狗家阳谷昂赛

🔗
westcoastboy 2019-3-11 13:02:24 | 只看该作者
全局:
最后一题用递归,每当遇到括号时
回复

使用道具 举报

🔗
westcoastboy 2019-3-11 13:20:26 | 只看该作者
全局:
回复

使用道具 举报

全局:
请问楼主有后续消息么 已加米~
回复

使用道具 举报

🔗
孙行者 2019-5-15 12:38:18 | 只看该作者
全局:
第六题应当是用stack, 从左到右扫一遍,在从右到左扫一遍,顺便把括号除掉。每逢括号就把当前的字母串压入stack。

补充内容 (2019-5-15 12:39):
第一步要先把字符串tokenize.
回复

使用道具 举报

🔗
giveforce 2019-5-29 09:34:21 | 只看该作者
全局:
第六题用递归的方式写了一下

  1. def process(str, start, end, parenthesisMatch):
  2.     # Separate the string by commas on the same level
  3.     i, j = start, start
  4.     list = []
  5.     while j <= end:
  6.         if str[j] == ',':
  7.             list.append((i, j))
  8.             i = j + 1
  9.         elif str[j] == '{':
  10.             j = parenthesisMatch[j]
  11.         j += 1
  12.     if i<j:
  13.         list.append((i, j))
  14.    
  15.     # Parse each separated string (recursive)
  16.     res = []
  17.     for i, j in list:
  18.         if str[i:j].isalpha():
  19.             res.append(str[i:j])
  20.         else:
  21.             ii, jj = i, i
  22.             super = [""]
  23.             while jj<j:
  24.                 if str[jj] == '{':
  25.                     res_before = process(str, ii, jj-1, parenthesisMatch)
  26.                     res_in_paren = process(str, jj+1, parenthesisMatch[jj]-1, parenthesisMatch)
  27.                     if res_before:
  28.                         for k in range(len(res_in_paren)):
  29.                             res_in_paren[k] = res_before[0] + res_in_paren[k]
  30.                     super_new = []
  31.                     for s in super:
  32.                         for t in res_in_paren:
  33.                             super_new.append(s+t)
  34.                     super = super_new
  35.                     jj = parenthesisMatch[jj]
  36.                     ii = jj + 1
  37.                 jj += 1
  38.             if ii<jj:
  39.                 for k in range(len(super)):
  40.                     super[k] += str[ii:jj]
  41.             res.extend(super)
  42.     return res

  43. def interpret(str):
  44.     parenthesisMatch = {} # Key: index of left parenthesis, value: index of right parenthesis
  45.     stack = []
  46.     for i, c in enumerate(str):
  47.         if c == '{':
  48.             stack.append(i)
  49.         elif c == '}':
  50.             parenthesisMatch[stack.pop()] = i
  51.     return process(str, 0, len(str) - 1, parenthesisMatch)
  52.    
  53. print(interpret('{a,b}x{c,d}y{e,f{g,h}{i,j}k}'))
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表