python 考试小计

来源:互联网 发布:macbook运行java 编辑:程序博客网 时间:2024/05/18 13:43

题目内容:

某些英语单词的字母经过重新排列后,能获得另外一个单词,如可以将“cinema”转换成另一个单词“iceman”。编写程序,将词表中由相同字母组成的单词聚成一类,并按照单词个数由多到少的顺序输出各类中的全部单词(每类占一行,单词按字典序由小到大排列,之间用空格分隔),若每类中单词个数相同,则按每类中第一个单词的字典序由大到小输出各个类别。


输入格式:

按字典序由小到大输入若干个单词,每个单词占一行。


输出格式:

并按照单词个数由多到少的顺序输出各类中的全部单词(每类占一行,单词按字典序由小到大排列,之间用空格分隔),若每类中单词个数相同,则按每类中第一个单词的字典序由大到小输出各个类别。


输入样例:

cinema

iceman

maps

spam


输出样例:

maps spam

cinema iceman

时间限制:500ms内存限制:32000kb

#coding=utf-8def get_new_str_sort(s):    l = list(s)    l.sort()    s = "".join(l)    return sstopword = ''str = ''dict={}for line in iter(raw_input, stopword):  sort_s=get_new_str_sort(line)  if sort_s in dict:      dict[sort_s ].append(line)  else:      dict[sort_s ]=[ line ]lst=[]len_lst=[]new_dic={};for (k,v) in  dict.items():    #v.sort()    one_len=len(v)    if  one_len not in len_lst:        len_lst.append(one_len)    if one_len in new_dic:        new_dic[one_len].append( v[0] )    else:        new_dic[one_len]=[  v[0] ]len_lst.sort(reverse=True)all_word=[]for word_num in len_lst:    for keys in new_dic[word_num]:        new_keys=get_new_str_sort(keys);        print ' '.join(dict[new_keys]) 



1 0