中级农民
- 积分
- 297
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-7-1
- 最后登录
- 1970-1-1
|
第一题大概是个trie
- class Node:
- def __init__(self, content, times):
- self.content = content
- self.times = times
- self.children = {}
- self.isEnd = False
- website = [
- ['google.com', '40'],
- ['yahoo.com', '30'],
- ['sports.yahoo.com', '20'],
- ['sports.google.com', '500']
- ]
- head = root = Node("", 0)
- for w in website:
- sections = w[0].split('.')
- for i in range(len(sections) - 1, -1, -1):
- s = sections[i]
- if s not in root.children:
- root.children[s] = Node(s, int(w[1]))
- else:
- root.children[s].times += int(w[1])
- root = root.children[s]
- root.isEnd = True
- root = head
- res = []
- def dfs(node, cur = ""):
- for e in node.children:
- cur_node = node.children[e]
- cur_url = "." + cur_node.content + cur
- res.append((cur_url, cur_node.times))
- dfs(cur_node, cur_url)
- dfs(head, "")
- print(res)
复制代码
|
|