Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
There are only two possible formats. First, figure out how to reliably distinguish them. Be careful: a string starting with R is not automatically in RXCY form.
The RXCY style must match the exact pattern . In other words, something like R23C55 qualifies, but RC23 does not. If that pattern fails, it's the usual letters + digits format.
To convert from BC23-style to R23C55-style, split the string into the leading letters and trailing digits. The letters are a base- number with :
Converting the column number back to letters is the only slightly annoying part. Since there is no zero digit, do this loop:
Each digit gives a letter from A to Z, but you'll build the answer in reverse.
So the whole solution is just two cases:
x-- trick, and print letters + row.letters and row, convert letters to a number, and print R + row + C + number.This is straight-up per string. No fancy stuff, just careful parsing and one mildly cursed base- conversion.
Each cell is written in one of two notations:
BC23 = column BC, row 23R23C55 = row 23, column 55So the job is brutally simple in spirit: detect the current format, then convert to the other one.
The only mildly annoying part is the column notation. Excel columns are basically base — except, because spreadsheets like being a little weird, the digits are instead of .
A string is in RC-style iff it matches:
That means:
RCThis matters because not every string starting with R is RC-style. For example, RC23 is a perfectly valid Excel-style cell: column RC, row 23.
So the detection rule must be exact, not hand-wavy nonsense.
Suppose we have something like BC23.
Split it into:
BC23Now convert the letters to a number.
If the column is , then:
where
A standard iterative version is:
for each letter from left to right.
Example:
So BC23 becomes:
Suppose we have R23C55.
Split it into:
23Now convert back into Excel letters.
This is where people usually trip over the missing zero digit.
In normal base , digits would be . But here they are:
So before taking modulo, we shift by one:
Then extract the last letter:
And continue with:
Repeat until .
BCSo:
For each string :
letters + rowR + row + C + numberThere are two cases.
We split the string into its unique prefix of letters and suffix of digits. The suffix is the row. The prefix is interpreted as a base- number with digits , which is exactly how the problem defines spreadsheet columns. Therefore the computed column number is correct, and the output R[row]C[col] is the same cell in RC-style.
We parse the row and numeric column directly. The repeated transformation
x \leftarrow x-1,\quad digit = x \bmod 26,\quad x \leftarrow \lfloor x/26 \rfloor$$ is the standard way to write a positive integer in the alphabet system with digits $A\dots Z$ corresponding to $1\dots 26$. Reversing the extracted letters gives exactly the spreadsheet column label. Appending the row yields the same cell in Excel-style. So in both cases, the output is the other notation for the same cell. Done. No black magic, just annoying indexing. --- ## Complexity Let $L$ be the length of a coordinate string. Each string is processed in:O(L)
O\left(\sum |s|\right)
with $O(1)$ extra memory per test case. This absolutely crushes the limits.#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
bool isRC(const string& s) {
if (s.empty() || s[0] != 'R') return false;
int n = (int)s.size();
int i = 1;
while (i < n && isdigit((unsigned char)s[i])) i++;
if (i == 1 || i == n || s[i] != 'C') return false;
i++;
if (i == n) return false;
while (i < n && isdigit((unsigned char)s[i])) i++;
return i == n;
}
ll colToNum(const string& col) {
ll x = 0;
for (char c : col) {
x = x * 26 + (c - 'A' + 1);
}
return x;
}
string numToCol(ll x) {
string res;
while (x > 0) {
x--; // shift because A=1, ..., Z=26
res.push_back(char('A' + (x % 26)));
x /= 26;
}
reverse(res.begin(), res.end());
return res;
}
int main() {
setIO();
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (isRC(s)) {
int pos = 1;
while (isdigit((unsigned char)s[pos])) pos++;
string row = s.substr(1, pos - 1);
ll col = stoll(s.substr(pos + 1));
cout << numToCol(col) << row << '\n';
} else {
int pos = 0;
while (pos < (int)s.size() && isalpha((unsigned char)s[pos])) pos++;
string col = s.substr(0, pos);
string row = s.substr(pos);
cout << 'R' << row << 'C' << colToNum(col) << '\n';
}
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
bool isRC(const string& s) {
if (s.empty() || s[0] != 'R') return false;
int n = (int)s.size();
int i = 1;
while (i < n && isdigit((unsigned char)s[i])) i++;
if (i == 1 || i == n || s[i] != 'C') return false;
i++;
if (i == n) return false;
while (i < n && isdigit((unsigned char)s[i])) i++;
return i == n;
}
ll colToNum(const string& col) {
ll x = 0;
for (char c : col) {
x = x * 26 + (c - 'A' + 1);
}
return x;
}
string numToCol(ll x) {
string res;
while (x > 0) {
x--; // shift because A=1, ..., Z=26
res.push_back(char('A' + (x % 26)));
x /= 26;
}
reverse(res.begin(), res.end());
return res;
}
int main() {
setIO();
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (isRC(s)) {
int pos = 1;
while (isdigit((unsigned char)s[pos])) pos++;
string row = s.substr(1, pos - 1);
ll col = stoll(s.substr(pos + 1));
cout << numToCol(col) << row << '\n';
} else {
int pos = 0;
while (pos < (int)s.size() && isalpha((unsigned char)s[pos])) pos++;
string col = s.substr(0, pos);
string row = s.substr(pos);
cout << 'R' << row << 'C' << colToNum(col) << '\n';
}
}
return 0;
}