Google code jam exercise -- All Your Base

Source: Internet
Author: User

I can't solve a few questions in a row. I want to find a simple question to increase my sense of accomplishment. So after reading this question, the first question, although it was in the C round, should not be too difficult? But after reading the question, I knew that I was wrong. Why can't I even understand the question?

Helpless.

The answer is simple, and even Python code is provided. Perform a simple analysis.

First explain the meaning of the question, the question is given a string, contains a a-z0-9, represents a number, the base of this number can be any, requires a base, so that the number of the string represents the minimum, and the first digit is not 0.

How can we minimize the base? Base = 10, which is in decimal format. There are 0-9 values indicating 0-9. You can also use a-j to represent 0-9, which requires 10 different characters. The minimum base of a string can be the number of different characters in the string. To minimize the number of characters, the smaller the number, the better. In this way, the first character represents 1, and if the second character is different, it represents 2, and so on. Finally, convert the string to the decimal number and output it.

The Code is as follows,

#!/usr/bin/python#encoding:UTF-8#Filename:Base.pyimport sysinname = "input.txt"outname = "output.txt"if len(sys.argv)>1:    inname = sys.argv[1]    outname = inname.rstrip(".in")    outname = outname + ".out"fin = open(inname,"r")fout = open(outname,"w")sys.stdin = finsys.stdout = foutN = int(sys.stdin.readline().strip())for qw in range(1, N+1):    print 'Case #%d:' % qw,    num = sys.stdin.readline().strip()    values = {num[0]: 1}    for c in num:        if c not in values:            sz = len(values)            if sz == 1:                values[c] = 0            else:                values[c] = sz    result = 0    base = max(len(values), 2)    for c in num:        result *= base        result += values[c]    print resultfin.close()fout.close()

Because it is the code on content analysis, the small and large case tests must have passed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.