python3如何处理输入数据?
查看原帖
python3如何处理输入数据?
649980
oxiansheng楼主2023/6/6 18:53
M, N, P = list(map(lambda x:int(x), input().split(' ')))
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

nom = dict()
i = 0
while True:
    try:
        name = input()
        if name not in nom:
            nom[name] = dict()
    except:
        break
    i += 1
    if i == M: 
        break

def valid(sentences):
    l, splitted = len(sentences.split(' ')), sentences.split(' ')
    if l == 5:
        if ((splitted[1] == 'I' and splitted[2] == 'am') or (splitted[1] in nom and splitted[2] == 'is'))\
              and splitted[3] == 'not' and splitted[4] == 'guilty.':
            return True
    elif l == 4:
        if ((splitted[1] == 'I' and splitted[2] == 'am') or (splitted[1] in nom and splitted[2] == 'is'))\
              and splitted[3] == 'guilty.':
            return True
        if splitted[1] == 'Today' and splitted[2] == 'is' and splitted[3].rstrip('.') in weekdays:
            return True
    return False

j = 0
while True:
    try:
        sentence = input()
        splitted = sentence.split(' ')
        if not valid(sentence):
            continue
        name = splitted[0].rstrip(':')
        if sentence.endswith('not guilty.'):
            if 'unguilty' not in nom[name]:
                nom[name]['unguilty'] = set()
            nom[name]['unguilty'].add(splitted[1] if splitted[1] != 'I' else name)
        elif sentence.endswith('guilty.'):
            if 'guilty' not in nom[name]:
                nom[name]['guilty'] = set()
            nom[name]['guilty'].add(splitted[1] if splitted[1] != 'I' else name)
        else:
            if 'day' not in nom[name]:
                nom[name]['day'] = set()
            nom[name]['day'].add(splitted[-1].rstrip('.'))
    except:
        break
    j += 1
    if j == P: 
        break

names = list(nom.keys())
def check(nom, murderer, day):
    liar, honest = set(), set()
    for name in names:
        if 'guilty' in nom[name]:
            if len(nom[name]['guilty']) > 1 and murderer in nom[name]['guilty']:
                return murderer, day, False
            elif len(nom[name]['guilty']) > 1 and murderer not in nom[name]['guilty']:
                liar.add(name)
            elif len(nom[name]['guilty']) == 1 and murderer in nom[name]['guilty']:
                honest.add(name)
            elif len(nom[name]['guilty']) == 1 and murderer not in nom[name]['guilty']:
                liar.add(name)
        if 'unguilty' in nom[name]:
            if murderer in nom[name]['unguilty'] and len(nom[name]['unguilty']) == 1:
                liar.add(name)
            elif len(nom[name]['unguilty']) >= 1 and murderer not in nom[name]['unguilty']:
                honest.add(name)
            elif len(nom[name]['unguilty']) > 1 and murderer in nom[name]['unguilty']:
                return murderer, day, False
        if 'day' in nom[name]:
            if len(nom[name]['day']) == 1 and day in nom[name]['day']:
                honest.add(name)
            elif len(nom[name]['day']) >= 1 and day not in nom[name]['day']:
                liar.add(name)
            elif len(nom[name]['day']) > 1 and day in nom[name]['day']:
                return murderer, day, False
        
        if name in liar and name in honest:
            return murderer, day, False
        
    if len(liar) > N or len(honest) > M-N:
        return murderer, day, False
    return murderer, day, True


crimes = set()
for name in names:
    for day in weekdays:
        murderer, day, yes = check(nom, name, day)
        if yes:
            crimes.add(name)

if len(crimes) == 1:
    print(list(crimes)[0])
elif len(crimes) == 0:
    print('Impossible')
else:
    print('Cannot Determine')

这段代码只通过了三个用例,使用https://www.luogu.com.cn/paste/4kuk7qu2的数据在本地测是没有问题的

2023/6/6 18:53
加载中...