Spreadsheet (Codeforces 1B)
Tags: Implement, math

In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input: The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output: Write n lines, each line should contain a cell coordinates in the other numeration system.


Sample Inputs:
2
R23C55
BC23

Sample Outputs:
BC23
R23C55

Solution (Python):
import re
BASE = len('ABCDEFGHIJKLMNOPQRSTUVWXYZ')

def s2n(s):
    n = 0
    for c in s:
        n *= BASE
        n += ord(c) - ord('A') + 1
    return n

def n2s(n):
    s = ''
    while n > 0:
        s = chr((n - 1) % BASE + ord('A')) + s
        n = (n - 1) // BASE
    return s

def convert(s):
    (A, B, C) = re.search('^(\\D+)(\\d+)(?:\\D(\\d+))?$', s).groups()
    if C is None:
        return f'R{B}C{s2n(A)}'
    return f'{n2s(int(C))}{B}'
results = [convert(input()) for _ in range(int(input()))]
print('\n'.join(results))

Solution (Python):
import re
BASE = len('ABCDEFGHIJKLMNOPQRSTUVWXYZ')

def s2n(s):
    n = 0
    for c in s:
        n *= BASE
        n += ord(c) - ord('A') + 1
    return n

def n2s(n):
    s = ''
    while n > 0:
        s = chr((n - 1) % BASE + ord('A')) + s
        n = (n - 1) // BASE
    return s

def convert(s):
    (A, B, C) = re.search('^(\\D+)(\\d+)(?:\\D(\\d+))?$', s).groups()
    if C is None:
        return f'R{B}C{s2n(A)}'
    return f'{n2s(int(C))}{B}'
last_results = [convert(input()) for _ in range(int(input()))]
print('\n'.join(last_results))

Solution (Python):
def col_num_to_label(col_num):
    col_label = ''
    while col_num > 0:
        col_num -= 1
        remainder = col_num % 26
        col_label = chr(65 + remainder) + col_label
        col_num //= 26
    return col_label

def label_to_col_num(col_label):
    col_num = 0
    for char in col_label:
        col_num = col_num * 26 + (ord(char) - 65 + 1)
    return col_num

def convert_coordinates(coord):
    if coord[0] == 'R' and coord[1].isdigit() and ('C' in coord):
        (row, col) = coord.split('R')[1].split('C')
        col_num = int(col)
        col_label = col_num_to_label(col_num)
        return col_label + row
    else:
        col_label = ''
        row = ''
        for char in coord:
            if char.isalpha():
                col_label += char
            else:
                row += char
        col_num = label_to_col_num(col_label)
        return 'R' + row + 'C' + str(col_num)
n = int(input())
coordinates = [input() for _ in range(n)]
for coord in coordinates:
    print(convert_coordinates(coord))