一时兴起用Python写了写,发现以前看过的书全忘光了,输入输出的格式坑死我了。。语法几乎每行都要google,写了半天终于拼出了份丑陋的代码,凑和着AC了
import sys
def print_tree():
if(not first):
print
keys = trees.keys();
keys.sort()
for key in keys:
print '%s %.4f' % (key, float(trees.get(key)) * 100 / total)
stdin = sys.stdin
line = stdin.readline()
trees = dict()
total = 0
first = True
while(True):
if(not line):
break
elif(not line.strip()):
print_tree()
first = False
trees.clear()
total = 0
else:
total += 1
line = line.strip()
if(trees.has_key(line)):
trees[line] += 1
else:
trees[line] = 1
line = stdin.readline()
print_tree()