Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4086550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:44:30+00:00 2026-05-20T18:44:30+00:00

TLE always happen in SBANK SPOJ using python. In order to solve it, i

  • 0

TLE always happen in SBANK SPOJ using python. In order to solve it, i have to sort dict(), though dict() has huge number of KEYS(maximum–100000). Use sorted() function in my code takes no effect. Is there any fast solution? Thanks for your help.

My code below:

for j in range(n): # n is the number of keys
        account = sys.stdin.readline().rstrip()
        dic.setdefault(account, 0)
        dic[account] += 1
sorted(dic) # **this sort take a lot of time**

EDIT1:According to Justin Peel’s tips, i update my code below, but return still TLE. How can i do?

import sys
import psyco # import psyco module to speed up
psyco.full()
nCase = int(sys.stdin.readline().split()[0])
for i in range(nCase):
    n = int(sys.stdin.readline().split()[0])
    dic = dict()
    lst = list()
    for j in range(n):
        account = sys.stdin.readline().rstrip()
        dic.setdefault(account, 0)
        dic[account] += 1
    sys.stdin.readline()
    lst = dic.keys() # store keys in list
    lst.sort()
    for account in lst:
        sys.stdout.write('%s %s\n' % (account, dic[account]))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:44:31+00:00Added an answer on May 20, 2026 at 6:44 pm

    I was able to solve this problem. Here are some tips:

    1. Use Python 2.5. It is much faster than Python 3.2 which is the other option available on SPOJ with Python. Only one person has been able to get a fast enough solution using Python 3.2
    2. Just use a basic dict for counting. You can get by with the defaultdict from the collections module too, but the basic dict was faster for me.
    3. Sort only the keys of the dict, not the key-item pairs. Forming the key-item pairs takes far too long. Also, use keys = mydict.keys(); keys.sort() because it is the fastest way to go about it.
    4. Use psyco (pretty much always with SPOJ problems in Python)
    5. Learn the fastest ways to do input and output in Python. Hint: it isn’t iterating over every line of input for example.
    6. Try submitting after you’ve added each part (getting input, counting, doing output) to see where you are with time. This is a very valuable thing to do on SPOJ. The SPOJ computer running your code is quite likely much slower than your current computer and it can be hard to determine based on your own computer’s running time of the code if it will be fast enough for SPOJ.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree's with lazy propagation. I
I am trying to solve SPOJ problem 5: find the next largest integer palindrome
I have some code which was developed on a Windows 7 computer and runs
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I have a text file including the lines below: /* MY TXT File LINE
I've a UserManager Model.it's looks like: public class UserManager { private ToLetDBEntities TLE =
while submitting a solution for practise problem 6(odd) i got TLE error but while
My teacher have given us a acm problem about a math problem. I've tried
i got a problem with getting ManyToMany field values in template, here is the
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <application name=foo> <movie name=tc english=tce.swf chinese=tcc.swf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.