
输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
输出样例:
5 3 2
2 3 5
B B
本题有几个地方需要注意:
- 当甲乙没有赢,怎么输出,正确答案是都输出B,这个在题目中找不出线索,有点坑;
- 使用python超时,优化考虑的有三点,1⃣️使用sys.stdin作为输入,尤其是在大量输入数据时;2⃣️以空间换时间,使用一维或者多维list建立索引;3⃣️将执行代码放到函数中,至于为什么这样可以提高效率,主要基于cpython的机制问题,大概是因为局部变量和全局变量存取效率不一样;
- pta中python超市一般出现在用for循环输入大量数据时,可以考虑上面的优化策略;
- pta这种在线题目的三板斧是:1⃣️把题目需要的输入输出准确信息准确提炼,映射到合适的数据结构;2⃣️保证样例正确,把主流逻辑调通;3⃣️自己设计一些和0、1相关的边界数据,把边界条件调通,什么数组越界、函参不匹配大概都在这一步;4⃣️提交测试,根据返回信息再次调试,如果是错误问题那就需要检查主逻辑和边界条件,如果超时就使用上面的优化策略。
# -*- coding: utf-8 -*-
from collections import defaultdict
import sys
stat = {
'C B': -1,
'C J': 1,
'B C': 1,
'B J': -1,
'J C': -1,
'J B': 1,
'C C': 0,
'B B': 0,
'J J': 0}
def comp(x):
return '' + str(x[1]) + str(ord('z') - ord(x[0]))
if __name__ == '__main__':
result = defaultdict(int)
win_stat_0 = defaultdict(int)
win_stat_1 = defaultdict(int)
n = eval(sys.stdin.readline().strip())
for i in range(n):
input_str = sys.stdin.readline().strip()
result[stat[input_str]] += 1
if stat[input_str] == 1:
win_stat_0[input_str[0]] += 1
elif stat[input_str] == -1:
win_stat_1[input_str[2]] += 1
print(result[1], result[0], result[-1])
print(result[-1], result[0], result[1])
if len(win_stat_0) > 0:
print(sorted(win_stat_0.items(), key=comp, reverse=True)[0][0], end='')
else:
print('B', end='')
print(' ', end='')
if len(win_stat_1) > 0:
print(sorted(win_stat_1.items(), key=comp, reverse=True)[0][0])
else:
print('B')

323

被折叠的 条评论
为什么被折叠?



