-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlphabet2Integer.cpp
More file actions
141 lines (121 loc) · 3.19 KB
/
Alphabet2Integer.cpp
File metadata and controls
141 lines (121 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
AUTHOR : Peeyush Yadav
Problem : http://codeforces.com/contest/1/problem/B
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(a,b,c) for(int a=b;a<c;a++)
#define s(x) scanf("%d",&x);
#define sl(x) scanf("%lld",&x);
#define p(x) printf("%d\n",x);
#define p2(x,y) printf("%d %d\n",x,y);
#define pl(x) printf("%lld\n",x);
#define pl2(x,y) printf("%lld %lld\n",x,y);
#define p1d(a,n) for(int ix=0;ix<n;ix++) printf("%d ",a[ix]); printf("\n");
#define p2d(a,n,m) for(int ix=0;ix<n;ix++){ for(int jx=0;jx<m;jx++) printf("%d ",a[ix][jx]); printf("\n");}
void input(){
#ifdef Megamind
#define DEBUG
#define TRACE
freopen("inp.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
}
#ifdef TRACE
#define trace(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define trace2(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define trace3(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
#else
#define trace(x)
#define trace2(x,y)
#define trace3(x,y,z)
#endif
inline ll power(ll a, ll b, ll m) {
ll r = 1;
while(b) {
if(b & 1) r = r * a % m;
a = (a * a)% m;
b >>= 1;
}
return r;
}
inline ll power(ll a, ll b) {
ll r = 1;
while(b) {
if(b & 1) r = r * a ;
a = a * a;
b >>= 1;
}
return r;
}
/*........................................................END OF TEMPLATES.......................................................................*/
int main(){
input();
int t;
s(t);
char inp[30];
string ans;
while(t--){
scanf("%s",inp);
ans="";
int len = 0;
bool flag = 0;
if(inp[0] == 'R') {
if(inp[1] >='0' && inp[1] <='9'){
f(i,2,strlen(inp))
if(inp[i]=='C') flag = 1;
}
}
if(flag){
int len = strlen(inp),numlen = 1,num=0,off=1;
int i =0;
while(inp[i] != 'C') i++;
f(x,i+1,len) num = num*10 + inp[x] - '0';
while(off*26 < num) {
off*=26;
num -= off;
numlen++;
}
num--;
//trace2(num,numlen)
f(i,0,numlen){
off = 1;
f(j,1,numlen-i) off*=26;
ans += 'A' + (num/off);
num -= (num/off)*off;
}
i=1;
while(inp[i] != 'C') {
ans+=inp[i];
i++;
}
}
else{
ans+='R';
f(i,0,strlen(inp))
if(inp[i]>='0' && inp[i] <= '9') ans+=inp[i];
else len = i;
ans+='C';
int outputval = (26*(power(26,len)-1))/25;
//trace(outputval)
f(i,0,len) {
outputval += (inp[i]-'A')*power(26,len-i);
// trace(outputval)
}
// trace(outputval)
outputval += inp[len] - 'A' + 1;
string temp = "";
while(outputval){
temp+= (outputval%10) + '0';
outputval/=10;
}
reverse(temp.begin(),temp.end());
ans+=temp;
}
cout<<ans<<endl;
}
#ifdef Megamind
cout << "\nTime elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
}