文章目录
A - Spreadsheets
CodeForces - 1B
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 ≤ 10^5), 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 10^6 .
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Examples
Input
2
R23C55
BC23
Output
BC23
R23C55
这道题最难的地方就是比较鬼畜的进制,每次进位的时候需要进行减1。
我的做法比较麻烦,先判断是哪一种形式,再进行转换。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
using namespace std;
void dfs(int N) {
if (N > 26) dfs((N - 1) / 26);
putchar('A' + (N - 1) % 26);
}
int main(void)
{
int n, len, ro, co;
bool a, b, c, d;
char str[55], row[55], col[55], col2[55];
scanf("%d", &n);
getchar();
while (n--)
{
ro = 0; co = 0;
memset(row, 0, sizeof row);
memset(col, 0, sizeof col);
memset(col2, 0, sizeof col2);
a = 0; b = 0; c = 0; d = 0;
gets(str);
len = strlen(str);
for (int i = 0; i < len; i++)
{
if (!a && str[i] >= 'A' && str[i] <= 'Z')
a=1;
else if (a && !b && str[i] >= '0' && str[i] <= '9')
b=1;
else if (a && b && !c && str[i] >= 'A' && str[i] <= 'Z')
c=1;
else if (a && b && c && str[i] >= '0' && str[i] <= '9')
{
d=1;
strcpy(col, str + i);
break;
}
}
if (!c && !d)
{
//分开行和列
for (int i = 0; i < len; i++)
{
if (isalpha(str[i]))
col[i] = str[i];
else if (isalnum(str[i]))
{
strcpy(row, str + i);
break;
}
}
//计算列
int base = 1;
for (int i = strlen(col) - 1; i >= 0; i--)
{
co += (str[i] - 'A' + 1) * base;
base *= 26;
}
printf("R%sC%d\n", row, co);
}
else if (c && d)
{
bool flag = 0;
//计算行
for (int i = 1; i < len; i++)
{
if (isalpha(str[i]))
flag = 1;
else if (!flag && isalnum(str[i]))
row[i - 1] = str[i];
else if (flag && isalnum(str[i]))
{
strcpy(col, str + i);
break;
}
}
co = atoi(col);
int index = 0;
//转换成字符
dfs(co);
printf("%s\n", row);
}
}
return 0;
}
学姐的代码,比我写的简洁多了。我还是太菜了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void change(int x)
{
if (x)
{
change((x - 1) / 26);
putchar('A' + (x - 1) % 26);
}
}
int main(void)
{
int T;
char s[105];
for (scanf("%d", &T); T; T--)
{
int row, col;
scanf("%s", s);
if (sscanf(s, "%*[A-Z]%d%*[A-Z]%d", &row, &col) == 2)
{
change(col);
printf("%d\n", row);
}
else
{
col = 0;
for (int i = 0; isupper(s[i]); ++i)
col = col * 26 + s[i] - 'A' + 1;
printf("R%dC%d\n", row, col);
}
}
return 0;
}
B - Ancient Berland Circus
CodeForces - 1C
链接:https://blog.csdn.net/weixin_43772166/article/details/100116389
C - Longest Regular Bracket Sequence
CodeForces - 5C
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 10^6.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing “0 1”.
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1
以下是改编的杨dalao的代码。
栈内记录的是每个括号的下标,如果遍历到左半部分就进栈,遍历到右半部分且栈顶是左半部分就出栈。
下面的代码可以解决只有一种括号的情况,还可以解决多种括号且平衡的情况。但是如果遇到多组括号不平衡的话,就会返回错误的结果。这个时候就需要用到区间dp。
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
const int N = 1e6 + 10;
char s[N];
stack<int> st;
int main()
{
cin >> s;
int ans = 0, num = 0;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
if(s[i] == ')' && st.size() && s[st.top()] == '(')
st.pop();
else if(s[i] == ']' && st.size() && s[st.top()] == '[')
st.pop();
else if(s[i] == '}' && st.size() && s[st.top()] == '{')
st.pop();
else
st.push(i);
if(st.size())
{
if(ans == i - st.top())
num++;
else if(ans < i - st.top())
num = 1;
ans = max(ans, i - st.top());
}
else
{
if(ans == i + 1)
num++;
else if(ans < i + 1)
num = 1;
ans = max(ans, i + 1);
}
}
if(ans == 0)
num = 1;
cout << ans << ' ' << num << endl;
return 0;
}