最长的顺子

def find_longest_chain():
    # 输入处理
    cur_pokers = input().split('-')
    past_pokers = input().split('-')
    
    # 牌值映射(只需要单向)
    value_map = {'3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10,
                'J':11, 'Q':12, 'K':13, 'A':14}
    reverse_map = {v:k for k,v in value_map.items()}
    
    # 初始化剩余牌计数
    remain_counts = {v:4 for v in value_map.values()}
    
    # 统计已出的牌
    for poker in cur_pokers + past_pokers:
        if poker != '2' and poker in value_map:
            remain_counts[value_map[poker]] -= 1
    
    # 收集剩余的牌值并排序
    remaining = sorted([v for v in remain_counts if remain_counts[v] > 0])
    
    # 查找最长顺子
    max_chain = []
    current_chain = []
    
    for val in remaining:
        if not current_chain:
            current_chain.append(val)
        else:
            if val == current_chain[-1] + 1:
                current_chain.append(val)
            else:
                if len(current_chain) >= 5 and len(current_chain) > len(max_chain):
                    max_chain = current_chain.copy()
                current_chain = [val]
    
    # 检查最后的顺子
    if len(current_chain) >= 5 and len(current_chain) > len(max_chain):
        max_chain = current_chain
    
    # 如果有多个相同长度的顺子,取最大的
    if max_chain:
        # 转换为牌面表示
        result = '-'.join(reverse_map[v] for v in max_chain)
        return result
    else:
        return "NO-CHAIN"

# 执行并输出结果
print(find_longest_chain())

优化一下:

my_poker = list(input().split('-'))
past_poker = list(input().split('-'))
#扑克牌的标准顺序
order = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', 'JOKER']
pokers = {
    '3':4,'4':4,'5':4,'6':4,'7':4,'8':4,'9':4,'10':4,'J':4,'Q':4,'K':4,'A':4,'JOKER':0
}
for num in my_poker:
    if num in pokers:
        pokers[num]-=1
for num in past_poker:
    if num in pokers:
        pokers[num]-=1
max_L=0
max_key=''
L = 0
for key,value in pokers.items():
    if value>0:
        L+=1
    else:
        if max_L<=L:
            max_L =  L
            max_key = key
        L = 0
if max_L>4:
    end_index = order.index(max_key)-1
    start_index = end_index-max_L+1
    sequence = order[start_index:end_index+1]
    print('-'.join(sequence))
else:
    print('NO-CHAIN')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值