2015年4月20日 星期一

2015/04/20 Codeforces 1B. Spreadsheets

/* 
    寫完看了別人的code
    才發現是變形的26進位。。。
    自己寫的爆長 別人的寫法好精妙Q__Q
    http://codeforces.com/contest/1/submission/2521337
*/
// http://codeforces.com/problemset/problem/1/B
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char c[200000]; int cn;
char d[200000]; int dn;
int num;

void solve1()
{
    sscanf(c, " %[A-Z] %d", d, &num);
    dn = strlen(d);

    int p = 1, pwd = 26;
    for(int di = 1; di < dn; di++)
        p += pwd, pwd *= 26;
    pwd/=26;

    for(int di = 0; di < dn; di++)
        p += (d[di]-'A')*pwd, pwd /= 26;

    printf("R%dC%d\n", num, p);
}

void solve2()
{
    int x, y;
    sscanf(c, " R%dC%d", &x, &y);

    int d = 1, pwd = 26, cnt = 1;
    while( cnt+pwd < y ) cnt += pwd, pwd*=26, d++;
    pwd/=26;
    y -= cnt;

    for(int di = 0; di < d-1; di++)
        printf("%c", y/pwd+'A'), y%=pwd, pwd/=26;
    printf("%c", y/pwd+'A');
    printf("%d\n", x);
}

int cnt()
{
    int re = 0;

    for(int ci = 0; ci < cn-1; ci++)
    {
        if( 'A' <= c[ci] && c[ci] <= 'Z' )
            if( '0' <= c[ci+1] && c[ci+1] <= '9'  ) re++;
    }

    return re;
}

int T;

int main()
{
    scanf("%d", &T);

    while( T-- )
    {
        scanf("%s", c);
        cn = strlen(c);

        if( cnt() == 1 ) solve1();
        else solve2();
    }
}

沒有留言:

張貼留言