fork(1) download
  1.  
  2. def allChars(a):
  3. return sum([ord(_) for _ in a])
  4. #======================
  5.  
  6. things=raw_input()#'stol stul dverj krovatj kompjuter'
  7. corrupts=raw_input()#'vatjkrotulsredvjlostkompterju'
  8.  
  9. print 'Initial:'
  10. print things
  11. print corrupts,'\n-----------------------------\n'
  12.  
  13. thingArr=things.split(' ')
  14. thingLen=list(set([len(_) for _ in thingArr]))
  15. thingDict={allChars(_):[0,_] for _ in thingArr}
  16.  
  17. i=0;j=0
  18. while i<len(corrupts):
  19. for l in thingLen:
  20. if allChars(corrupts[i:i+l]) in thingDict:
  21. thingDict[allChars(corrupts[i:i+l])][0]=j
  22. i+=l;j+=1
  23. break
  24.  
  25. thingResult=[_[1] for _ in sorted(thingDict.values())]
  26.  
  27. i=0;corruptResult=[]
  28. for l in [len(_) for _ in thingResult]:
  29. corruptResult.append(corrupts[i:i+l])
  30. i+=l
  31.  
  32. print 'Final:'
  33. print ' '.join(thingResult)
  34. print ' '.join(corruptResult)
  35.  
  36.  
Success #stdin #stdout 0.01s 7900KB
stdin
abab ab ba
ababaabb
stdout
Initial:
abab ab ba
ababaabb 
-----------------------------

Final:
ba abab
ab abaa