Python 3 version


#1
def main():
    f, d = {},{}
    for w in open('input').read().split():
        f[w] = f.get(w,0)+1
    for k, v in f.items():
        d[v] = d.get(v,[])+[k]
    for k,v in sorted(d.items()):
        print(k, ' '.join(sorted(v)))
if __name__ == '__main__':
    main()