专栏文章
题解:P13670 [GCPC 2023] Eszett
P13670题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miofthe1
- 此快照首次捕获于
- 2025/12/02 18:30 3 个月前
- 此快照最后确认于
- 2025/12/02 18:30 3 个月前
P13670 [GCPC 2023] Eszett 题解
原题链接
(1)题目分析:
此题的问题是对输入的大写字符串进行转换,规则如下:
- 大写字符串中的两个连续的 ‘S’ 转换为两个小写 ‘B’ ;
- 大写字符串中的两个连续的 ‘S’ 转换为两个小写 ‘s’ ;
- 将为转换的大写字母转为小写;
(2)核心思路:
- 通过递归生成连续含 ‘S’ 段的所有可能分解;
- 处理每个分块的转化结果;
- 组合所以分块可能性生成结果;
(3)完整代码实现:
AC记录
CPP#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
vector<string> g(int n) {
if (n == 0) return {""};
if (n == 1) return {"s"};
vector<string> result;
for (const auto& seq : g(n - 1)) {
result.push_back("s" + seq);
}
for (const auto& seq : g(n - 2)) {
result.push_back("B" + seq);
}
return result;
}
int main() {
string input;
cin >> input;
vector<string> tokens;
for (int i = 0; i < input.size();) {
if (input[i] == 'S') {
int j = i;
while (j < input.size() && input[j] == 'S') j++;
tokens.push_back(input.substr(i, j - i));
i = j;
} else {
int j = i;
while (j < input.size() && input[j] != 'S') j++;
tokens.push_back(input.substr(i, j - i));
i = j;
}
}
vector<vector<string>> pos;
for (const auto& token : tokens) {
if (token.find('S') == string::npos) {
string lowerToken;
for (char c : token) {
lowerToken += tolower(c);
}
pos.push_back({lowerToken});
} else {
pos.push_back(g(token.size()));
}
}
vector<string> results = {""};
for (const auto& block : pos) {
vector<string> ne;
for (const auto& res : results) {
for (const auto& seq : block) {
ne.push_back(res + seq);
}
}
results = move(ne);
}
for (const auto& res : results) {
cout << res << endl;
}
return 0;
}
完结,点个赞吧!
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...