Codeforces -- Spreadsheets (simulation)
Spreadsheets
Time Limit:10000 MS
Memory Limit:65536KB
64bit IO Format:% I64d & % I64uSubmit StatusAppoint description: System Crawler)
Description
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 numberN(1? ≤?N? ≤? 105), the number of coordinates in the test. Then there followNLines, 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
WriteNLines, each line shoshould contain a cell coordinates in the other numeration system.
Sample Input
Input
2R23C55BC23
Output
BC23R23C55
In the conversion between the two formats, the corresponding letter representation between 1 and 1000000 is processed first, and binary is used for conversion of letters into numbers.
#include
#include
#include using namespace std ;char str[1000010][6] ;int num[1000010] , low[10] , mid , high[10] ;char s[1000] ;int serch(char *s,int i){ int l = low[i] , m , h = high[i] , k ; while( l <= h ) { m = ( l + h ) / 2 ; k = strcmp(s,str[m]) ; if( k == 0 ) return m ; if( k < 0 ) h = m - 1 ; else l = m + 1 ; }}void init(){ int i , j , temp ; str[1][0] = 'A' ; str[1][ num[1] ] = '\0' ; num[1] = 1 ; low[1] = 1 ; for(i = 1 ; i <= 1000000 ; i++) { temp = 1 ; num[i] = num[i-1] ; for(j = num[i-1]-1 ; j >= 0 ; j--) { temp += (str[i-1][j] - 'A'+1) ; if( temp >= 27 ) { str[i][j] = temp - 27 + 'A' ; temp = 1 ; } else { str[i][j] = temp - 1 + 'A' ; temp = 0 ; } } if( temp ) { num[i]++ ; for(j = num[i]-1 ; j > 0 ; j--) str[i][j] = str[i][j-1] ; str[i][0] = 'A' ; high[ num[i-1] ] = i-1 ; low[ num[i] ] = i ; } str[i][ num[i] ] = '\0' ; } high[ num[1000000] ] = 1000000 ;}int main(){ int t , x , y , l , i , j ; init() ; scanf("%d", &t) ; while( t-- ) { scanf("%s", s) ; l = strlen(s) ; for(i = 0 ; i < l ; i++) if( s[i] >= '0' && s[i] <= '9' ) break ; for( ; i < l ; i++) if( s[i] >= 'A' && s[i] <= 'Z' ) break ; if( i < l ) { //RC x = 0 ; y = 0 ; for(i = 1 ; i < l ; i++) { if( s[i] == 'C' ) break ; x = x * 10 + s[i] - '0' ; } for(i++ ; i < l ; i++) y = y * 10 + s[i] - '0' ; printf("%s%d\n", str[y], x); } else { x = 0 ; y = 0 ; for(i = 0 ; i < l ; i++) if( s[i] >= '0' && s[i] <= '9' ) break ; for(j = i ; j < l ; j++) x = x * 10 + s[j] - '0' ; s[i] = '\0' ; y = serch(s,i) ; printf("R%dC%d\n", x, y) ; } } return 0;}