Link:
https://www.hackerrank.com/challenges/acm-icpc-team/submissions/code/11617807
1 def count_max_topics(konw_topics): 2 max_topics = 0 3 max_teams = 0 4 for i, person1 in enumerate(konw_topics): # enumer的用法學習 5 for j, person2 in enumerate(konw_topics[i+1:]): 6 bin1 = int(person1, 2) # 二進制的轉換 7 bin2 = int(person2, 2) 8 topics = bin(bin1 | bin2).count('1') 9 10 if topics > max_topics: # 找最大值的一個通用思路11 max_topics = topics12 max_teams = 113 elif topics == max_topics:14 max_teams += 115 16 PRint max_topics17 print max_teams18 19 def main():20 n, m = map(int, raw_input().strip().split(' ')) #雙位輸入的模板21 konw_topics = [] # 用list合適22 for _ in range(n):23 konw_topics.append(raw_input().strip())24 25 count_max_topics(konw_topics) # func設置分散,這樣更易讀和理解26 27 28 main()
問題本質
二進制運算
Subarray計算
循環記錄最大值
學習
enumerate
二進制的轉換 int(,2)
熟悉dash的使用
找最大值的思維通路
命名變量、函數更加規范
新聞熱點
疑難解答