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.