NTFY 超得屁(°∀°)ノ 2023-08-18 06:24 采纳率: 50%
浏览 7
已结题

CSDN每日一题求解

题目名称:豚鼠排名榜

时间限制: 1000ms 内存限制: 256M

题目描述

已知字符A.B,C。每个字符都有自己的权值q。 现不知道权值q,只知道A,B,C的三次比较结果。

输入描述

输入三个字符串表示三次比较的结果

输出描述

输出结果,如果不存在输出”Impossible”

示例
示例1

输入:


A < B
B > C
C > A

输出:

ACB
class Solution:
    def __init__(self) -> None:
        pass
    
    def solution(self, exp1, exp2, exp3):
        relationships = {'A': [], 'B': [], 'C': []}
        
        comparisons = [exp1, exp2, exp3]
        for comparison in comparisons:
            char1, char2 = comparison[0], comparison[2]
            if comparison[1] == '<':
                relationships[char1].append(char2)
            elif comparison[1] == '>':
                relationships[char2].append(char1)
        
        order = []
        visited = set()
        
        def dfs(char):
            visited.add(char)
            for next_char in relationships[char]:
                if next_char not in visited:
                    dfs(next_char)
            order.append(char)
        
        for char in relationships.keys():
            if char not in visited:
                dfs(char)
        
        if len(order) != 3:
            return "Impossible"
        
        return ''.join(order[-1::-1])


if __name__ == "__main__":
    exp1 = input().strip()
    exp2 = input().strip()
    exp3 = input().strip()
    
    sol = Solution()
    result = sol.solution(exp1, exp2, exp3)
    print(result)


通过率只有90%,不知道为啥?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-08-18 08:10
    关注

    【以下回答由 GPT 生成】

    问题已经给出了具体的解决方案,但是通过率只有90%。为了优化代码以提高通过率,我们可以对输入进行校验,确保输入的字符串符合要求。

    具体优化方案如下:

    1. 添加输入校验功能。我们可以在输入前添加一些校验逻辑,确保输入的字符串格式正确。比如判断输入的字符串长度是否为5,确保输入的第2个字符是"<"或者">"。

    优化后的代码如下:

    class Solution:
        def __init__(self) -> None:
            pass
    
        def solution(self, exp1, exp2, exp3):
            # 输入校验
            if not self.validate_input(exp1) or not self.validate_input(exp2) or not self.validate_input(exp3):
                return "Invalid input"
    
            relationships = {'A': [], 'B': [], 'C': []}
    
            comparisons = [exp1, exp2, exp3]
            for comparison in comparisons:
                char1, char2 = comparison[0], comparison[2]
                if comparison[1] == '<':
                    relationships[char1].append(char2)
                elif comparison[1] == '>':
                    relationships[char2].append(char1)
    
            order = []
            visited = set()
    
            def dfs(char):
                visited.add(char)
                for next_char in relationships[char]:
                    if next_char not in visited:
                        dfs(next_char)
                order.append(char)
    
            for char in relationships.keys():
                if char not in visited:
                    dfs(char)
    
            if len(order) != 3:
                return "Impossible"
    
            return ''.join(order[-1::-1])
    
        def validate_input(self, exp):
            if len(exp) != 5:
                return False
            if exp[1] != "<" and exp[1] != ">":
                return False
            return True
    
    
    if __name__ == "__main__":
        exp1 = input().strip()
        exp2 = input().strip()
        exp3 = input().strip()
    
        sol = Solution()
        result = sol.solution(exp1, exp2, exp3)
        print(result)
    

    这样,我们增加了输入校验的功能,确保输入符合要求的格式。重新运行代码,通过率应该会提高。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月26日
  • 已采纳回答 8月18日
  • 创建了问题 8月18日

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画