Ok, thanks. And your python submisson was accepted
So it must be my algorithm or my implementation that results in TLE.
Here's my code:[bbone=python,444]""" SPOJ (classical). 10815. Finally a Treat. Problem code: DCEPC207 """
import sys
import collections
import psyco
psyco.full()
def check_name(name):
freq = collections.defaultdict(int)
maxf = 0
maxc = ''
for c in name:
freq[c] += 1
if freq[c] > maxf:
maxf = freq[c]
maxc = c
elif freq[c] == maxf:
maxc = ''
return (freq, maxc)
def check_list(line):
names = line.split()
name_stats = [None]*len(names)
line_stats =collections.defaultdict(int)
maxf = 0
maxc = ''
for pos, name in enumerate(names):
freq, name_char = check_name(name)
name_stats[pos] = (name, name_char)
for k,v in freq.iteritems():
line_stats[k] += v
if line_stats[k] > maxf:
maxf = line_stats[k]
maxc = k
result = [n[0] for n in name_stats if n[1] == maxc]
return '\n'.join(result)
def main():
cases = sys.stdin.read().split('\n')
results = []
for num, line in enumerate(cases[1:]):
if line:
results.append('Case #%s:' % (num + 1))
results.append(check_list(line))
sys.stdout.write('\n'.join(results))
sys.stdout.write('\n')
if name == 'main':
main()[/bbone]
Can someone please give some hint where I should optimize it? Does psyco work well with defaultdict? Or is my approach not suitable at all?
Thanks for any comments!
Daft