题解 | 查找兄弟单词

def judge(s,target):
    l1=len(s)
    l2=len(target)
    temp1=s
    temp2=target
    temp1=list(set(temp1))
    temp2=list(set(temp2))
    temp1.sort()
    temp2.sort()
    c1=[]
    c2=[]
    if l1==l2 and temp1==temp2:#两个的个数相同是前提
        #要继续看内部的单词个数是否一样
        temp3=len(temp1)#单词种类数
        for i in range(temp3):
            c1.append(s.count(temp1[i]))
            c2.append(target.count(temp2[i]))
        if c1==c2:
            return True
        else:
            return False
    else:
        return False
while True:
    try:
        s=input().split()#输入单词链
        n=int(s[0])#输入的单词个数
        k=int(s[-1])#兄弟单词的位置
        v=s[1:-1:]#单词链
        target=v[-1]#目标单词
        other=v[:-1:]#字典单词
        #开始处理
        #首先进行排序
        other.sort()
        #寻找兄弟单词
        l=len(target)
        a=[]#兄弟单词存储
        for i in other:
            #print(i)
            if judge(i,target) and i!=target:
                a.append(i)
        a.sort()#以防万一再次排序
        print(len(a),end='\n')
        print(a[k-1])
    except:
        break


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务