寻找兄弟单词

来源:互联网 发布:slay的网络意思 编辑:程序博客网 时间:2024/06/10 12:19

输入描述:
先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n


输出描述:
根据输入,输出查找到的兄弟单词的个数

输入例子:
3 abc bca cab abc 1

输出例子:
2 bca
python编程实现:
# -*- coding:utf-8 -*-import sysdef findBrother(words,word,k):    brotherWords=[]    #从兄弟单词中找出第K个兄弟单词返回    charWord=sorted([c for c in word])    #print charWord    for item in words:        #将每个单词的字符进行分割        itemList=sorted([c for c in item])        #print itemList#==是弱等号 即只要元素以及位置相等,就返回true        if (charWord==itemList and item !=word):            brotherWords.append(item)    #对brotherWords列表进行排序 然后找出第K个兄弟    brotherWordList=sorted(brotherWords)    return len(brotherWordList),brotherWordList[k-1]if __name__ == '__main__':    try:        lines=sys.stdin.readline().split()        number=int(lines[0])        words=[]        for i in range(1,number+1):            words.append(lines[i])        #输入要寻找兄弟单词的单词        word=lines[-2]        #输入要寻找的第k个兄弟的k        k=int(lines[-1])        # print words,word,k        # print "=======================----"        brotherWordLen,brotherWord=findBrother(words,word,k)        print brotherWordLen,brotherWord    except:        pass


0 0