9.9头条笔试研发,骗了这么多分不知道是不是人工阅卷
最大不重复子串:用hash map维护一个滑动窗口,ac。
def lengthOfLongestSubstring(s): hash_table = {} max_len = -1 cur_left = 0 for i, c in enumerate(s): if c in hash_table and hash_table[c] >= cur_left: cur_left = hash_table[c] + 1 else: max_len = max(max_len, i - cur_left + 1) hash_table[c] = i return max_len
组织优化:暴力搜索,ac
import sys DIRS = [[1, 0], [-1, 0], [0, 1], [0, -1]] def dfs(g, x, y, visited): visited[x][y] = True for dir in DIRS: temp_x = x + dir[0] temp_y = y + dir[1] if 0 <= temp_x < len(g) and 0 <= temp_y < len(g[0]) and not visited[temp_x][temp_y] and g[temp_x][temp_y] == 1: dfs(g, temp_x, temp_y, visited) return if __name__ == '__main__': m = int(sys.stdin.readline().strip()) graph = [] for i in range(m): temp = list(map(int, sys.stdin.readline().strip().split())) if temp: graph.append(temp) visited = [[False for i in range(len(graph[0]))] for j in range(len(graph))] count = 0 for i in range(len(graph)): for j in range(len(graph[0])): if graph[i][j] == 1 and not visited[i][j]: dfs(graph, i, j, visited) count += 1 print(count)
ip还原:输出2骗了50%
utf-8校验:输出0骗了60%
抖音红人:将关系抽象成图后暴力搜索,ac。
import sys def dfs(g, x, y, visited): n = len(g) visited[y] = True for i in range(n): if i != y and g[y][i] == 1 and g[x][i] == 0 and not visited[i]: g[x][i] = 1 dfs(g, x, i, visited) return if __name__ == '__main__': n = int(sys.stdin.readline().strip()) m = int(sys.stdin.readline().strip()) temp_relation = list(map(int, sys.stdin.readline().strip().split())) relation = [[0 if i != j else 1 for i in range(n)] for j in range(n)] for i in range(m): relation[temp_relation[2 * i] - 1][temp_relation[2 * i + 1] - 1] = 1 for i in range(n): visited = [False for i in range(n)] for j in range(n): if i != j and relation[i][j] == 1 and not visited[j]: dfs(relation, i, j, visited) relation = list(zip(*relation)) count = 0 for i in range(n): flag = True for j in range(n): if relation[i][j] == 0: flag = False break if flag: count += 1 print(count)
#笔试题目##字节跳动##题解#