import sys
import tstree
fname= 'high_freq_site.list'tree=tstree.TernarySearchTrie()
tree.loadData(fname)
token= ''counter= 0post=[]
# url, count, posttimefor line insys.stdin:
line=line.strip()
arr=line.split()if len(arr) != 3:continue#print arr
num= arr[1]
url= arr[0]
posttime= int(arr[2])if token == '':
token=url
counter= 0counter+= int(num)
post.append(posttime)
elif token==url:
counter+= int(num)
post.append(posttime)
elif token!=url:
ret=tree.maxMatch(token)ifret and post:
print'%s\t%s\t%s\t%s' %(ret, token, counter, min(post))
token=url
counter= 0counter+= int(num)
post=[]
ret=tree.maxMatch(token)ifret and post:
print'%s\t%s\t%s\t%s' %(ret, token, counter, min(post))class TSTNode(object):
def __init__(self, splitchar):
self.splitchar=splitchar
self.data=None
self.loNode=None
self.eqNode=None
self.hiNode=Noneclass TernarySearchTrie(object):
def __init__(self):
self.rootNode=None
def loadData(self, fname):
f=open(fname)whileTrue:
line=f.readline()ifnot line:breakline=line.strip()
node=self.addWord(line)ifnode:
node.data=line
f.close()
def addWord(self, word):ifnot word:returnNone
charIndex= 0
ifnot self.rootNode:
self.rootNode= TSTNode(word[0])
currentNode=self.rootNodewhileTrue:
charComp= ord(word[charIndex]) -ord(currentNode.splitchar)if charComp == 0:
charIndex+= 1
if charIndex ==len(word):returncurrentNodeifnot currentNode.eqNode:
currentNode.eqNode=TSTNode(word[charIndex])
currentNode=currentNode.eqNode
elif charComp< 0:ifnot currentNode.loNode:
currentNode.loNode=TSTNode(word[charIndex])
currentNode=currentNode.loNodeelse:ifnot currentNode.hiNode:
currentNode.hiNode=TSTNode(word[charIndex])
currentNode=currentNode.hiNode
def maxMatch(self, url):
ret=None
currentNode=self.rootNode
charIndex= 0
whilecurrentNode:if charIndex >=len(url):breakcharComp= ord(url[charIndex]) -ord(currentNode.splitchar)if charComp == 0:
charIndex+= 1
ifcurrentNode.data:
ret=currentNode.dataif charIndex ==len(url):returnret
currentNode=currentNode.eqNode
elif charComp< 0:
currentNode=currentNode.loNodeelse:
currentNode=currentNode.hiNodereturnretif __name__ == '__main__':
import sys
fname= 'high_freq_site.list'tree=TernarySearchTrie()
tree.loadData(fname)for url insys.stdin:
url=url.strip()
ret=tree.maxMatch(url)
print ret