いろいろがんばりたいブログ

情報科学科の人がいろいろ書きます。

CF 1B Spreadsheets

http://codeforces.com/problemset/problem/1/B

なんか大変だった。よ。

#include <iostream>
#include <iomanip>

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <numeric>
#include <algorithm>

#include <functional>
#include <cctype>

#include <complex>
#include <string>
#include <sstream>

#define pb push_back
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()

#define rep(i,n) for(int i=0;i<(n);i++)
#define tr(container, it) \
    for(typeof(container.begin()) it = container.begin(); it != container.end(); ++it)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container),element) != container.end())
#define sz(a) ((int)a.size())

typedef long long ll;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
const double EPS = 1e-9;

using namespace std;

string solve(string s){
    bool rc = false;
    bool eigo = true;
    rep(i,s.length()){
        if(eigo){
            if(isdigit(s[i])) eigo = false;
        }else{
            if(isalpha(s[i])){
                rc = true;
                break;
            }
        }
    }
    int row=0,column=0;
    if(rc){
        stringstream ss;
        ss << s;
        char c;
        ss >> c >> row >> c >> column;
        ss.clear();
        while(true){
            int x = column % 26;
            if(x == 0){
                ss << 'Z';
                column = column / 26 - 1;
            }else{
                ss << (char)(column%26 + 'A' - 1);
                column = column / 26;
            }
            if(column == 0) break;
        }
        string cs;
        ss >> cs;
        reverse(all(cs));
        stringstream out;
        out << cs << row << endl;
        string outs;
        out >> outs;
        return outs;
    }else{
        rep(i,s.length()){
            if(isdigit(s[i])){
                row = row * 10 + s[i] -'0';
            }else{
                column = column * 26 + s[i]-'A'+1;
            }
        }
        stringstream ss;
        ss << "R" << row << "C" << column;
        string out;
        ss >> out;
        return out;
    }
}

int main(){
    int n;
    cin >> n;
    string s;
    rep(i,n){
        cin >> s;
        cout << solve(s) << endl;
    }
    return 0;
}