首页 > 代码库 > codeforces--Spreadsheets(模拟)
codeforces--Spreadsheets(模拟)
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 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 Input
2 R23C55 BC23
BC23 R23C55
在两种格式之间转换,先处理处了1到1000000的对应字母表示,对于字母转换为数字的时候使用二分
#include <cstdio> #include <cstring> #include <algorithm> 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; }
codeforces--Spreadsheets(模拟)