ACM ICPC Team

https://www.hackerrank.com/challenges/acm-icpc-team

get到了新用法itertools的一点点知识

另外复习了二进制的一些利用(好吧,其实已经忘光了,如果不用二进制来解题,系统总是会判断超时

import itertools
people, topic = raw_input().split()
people, topic = int(people), int(topic)
peopleList = []
for i in range(people):
 onePeople = raw_input()
 jinzhiPeople = int(onePeople,2)
 peopleList.append(jinzhiPeople)
Time = []
team = list(itertools.combinations(peopleList,2)) 
for item in team:
 topicTime = item[0] | item[1] 
 topicTime = bin(topicTime)
 topicTime = list(topicTime)
 Time.append(topicTime.count('1')) 
Time.sort()
MaxTopic = Time[len(Time) - 1]
print MaxTopic
MaxTeam = Time.count(MaxTopic)
print MaxTeam

下面这个做法被判断为超时= =|||

import itertools
people, topic = raw_input().split()
people, topic = int(people), int(topic)
peopleList = []
for i in range(people):
 onePeople = raw_input()
 peopleList.append(onePeople)
TopicTime = 0
TopicList = []
def isTopic(itemList, i):
 isTopicNum = False
 for item in itemList:
  if item[i] == '1':
   isTopicNum = True
 return isTopicNum
team = list(itertools.combinations(peopleList,2)) 
for item in team:
 TopicTime = 0
 for i in range(topic):
  istopicN = isTopic(item, i)
  if istopicN == True:
   TopicTime += 1
 TopicList.append(TopicTime)
 
TopicList.sort()
MaxTopic = TopicList[len(TopicList) - 1]
print MaxTopic
 
MaxTeam = 0
for item in team:
 TeamTopic = 0
 for i in range(topic):
  if item[1][i] == item[0][i]:
   if item[1][i] == '1':
    TeamTopic += 1
  else:
   TeamTopic += 1
  if TeamTopic == MaxTopic:
   MaxTeam += 1
print MaxTeam


你可能感兴趣的:(ACM ICPC Team)