U E D R , A S I H C RSS

Classify By Anagram/1002

hotspot 으로 프로파일링 돌린뒤 중간 쓸데없어보이는 코드들 마구마구 삭제. 가장 병목지점은 Anagram.register, ~cpp WordElement (지금은 input 갯수 n 에 대해 n 번 실행)

나중에 Psyco bind 하고 나서는 4.4 초.

P3 933, 128 RAM Win98 Python2.2 + Psyco 에서 돌림.

~cpp 
import os
import time
import psyco

class Formatter:
    def __init__(self, anAnagramTable, out=os.sys.stdout):
        for key in anAnagramTable.iterkeys():
            out.write(' '.join(anAnagramTable[key]) + "\n")

class Anagram:
    def __init__(self):
        self.anagrams={}

    def read(self, anIn=os.sys.stdin):        
        for word in anIn:
            self.register(word.strip())

    def getAnagrams(self):
        return self.anagrams
        
    def register(self, aWord):
        aw=''.join(WordElement(aWord))
        if self.anagrams.has_key(aw):
            self.anagrams[aw].append(aWord)
        else:
            self.anagrams[aw]=[aWord]

def WordElement(aWord):
    result=list(aWord)
    result.sort()
    return result

psyco.bind(WordElement) 
psyco.bind(Anagram)     
psyco.bind(Formatter)

if __name__=="__main__":
    start=time.clock()
    anagram=Anagram()
    anagram.read()
    end=time.clock()
    Formatter(anagram.getAnagrams())
    print "time : ", end-start
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:53
Processing time 0.0081 sec