B2027 题解代码
现代化 C++ 代码
C++#include <iostream>
#include <string>
#include <list>
#include <utility>
#include <tuple>
// 前向声明
class IOModule;
class CodeRunner;
// 内存类
class Memory final {
private:
std::list<char> memory; // 用链表存储内存,因为只会左右移动。
std::list<char>::iterator current; // 当前内存,利用迭代器。
void set_value(char value) noexcept; // 设置值
[[nodiscard]] char get_value() const noexcept; // 获取值
friend class IOModule; // 友元类,其可以访问私有成员
friend class CodeRunner;
public:
Memory() noexcept;
void move_left() noexcept;
void move_right() noexcept;
void inc_value() noexcept;
void dec_value() noexcept;
};
// IO 模块类
class IOModule final {
private:
std::string inputs;
std::string::iterator pointer;
Memory* memory;
public:
IOModule() = default; // 默认构造函数
IOModule(const std::string& v_inputs, Memory* v_memory) noexcept;
void read_input() noexcept;
void write_output() const noexcept;
};
// 代码运行类
class CodeRunner final {
private:
std::string codes;
std::string::iterator pointer;
Memory memory;
IOModule io;
void loop_begin() noexcept;
void loop_end() noexcept;
public:
CodeRunner(std::string& v_codes, std::string& v_inputs) noexcept;
void run() noexcept;
};
// 代码读取类
class CodeReader final {
private:
[[nodiscard]] static std::string read_codes() noexcept;
[[nodiscard]] static std::string read_inputs() noexcept;
public:
[[nodiscard]] static std::tuple<std::string, std::string> read() noexcept;
};
int main() {
auto [codes, inputs] = CodeReader::read(); // C++17 结构化绑定
CodeRunner runner(codes, inputs);
runner.run();
return 0;
}
Memory::Memory() noexcept {
memory.push_back('\0'); // 初始一片内存
current = memory.begin(); // 初始内存
}
void Memory::move_left() noexcept {
if (current == memory.begin()) { // 如果已经是最左边的内存了
memory.push_front('\0');
current = memory.begin();
} else {
current--;
}
}
void Memory::move_right() noexcept {
if (auto compare_it = --(memory.end()); current == compare_it) { // 如果已经是最右边的内存了
memory.push_back('\0');
current = memory.end();
current--; // 注意超尾迭代器
} else {
current++;
}
}
void Memory::inc_value() noexcept {
(*current)++;
}
void Memory::dec_value() noexcept {
(*current)--;
}
void Memory::set_value(char value) noexcept {
*current = value;
}
[[nodiscard]] char Memory::get_value() const noexcept {
return *current;
}
IOModule::IOModule(const std::string& v_inputs, Memory* v_memory) noexcept :
inputs(std::move(v_inputs)), // 移动语义,速度更快
pointer(inputs.begin()),
memory(v_memory) { }
void IOModule::read_input() noexcept {
if (pointer == inputs.end()) {
memory->set_value(-1);
} else {
memory->set_value(*pointer);
pointer++;
}
}
void IOModule::write_output() const noexcept {
std::cout.put(memory->get_value());
}
CodeRunner::CodeRunner(std::string& v_codes, std::string& v_inputs) noexcept :
codes(std::move(v_codes)), // 移动语义,速度更快
pointer(codes.begin()),
memory(),
io(v_inputs, &memory) { }
void CodeRunner::run() noexcept {
while (pointer != codes.end()) {
switch (*pointer) {
case '>': {
memory.move_right();
break;
}
case '<': {
memory.move_left();
break;
}
case '+': {
memory.inc_value();
break;
}
case '-': {
memory.dec_value();
break;
}
case ',': {
io.read_input();
break;
}
case '.': {
io.write_output();
break;
}
case '[': {
loop_begin();
break;
}
case ']': {
loop_end();
break;
}
}
pointer++;
}
}
void CodeRunner::loop_begin() noexcept {
if (memory.get_value() == 0) { // 看看要不要跳过
int count = 1;
pointer++;
while (count != 0) {
if (*pointer == '[') {
count++;
} else if (*pointer == ']') {
count--;
}
pointer++;
}
pointer--; // 此时 pointer 指向 ] 符号,再从 run 的循环加一后,就可以指向 ] 的下一个符号。
}
}
void CodeRunner::loop_end() noexcept {
int count = 1;
pointer--;
while (count != 0) {
if (*pointer == ']') {
count++;
} else if (*pointer == '[') {
count--;
}
pointer--;
}
// 此时 pointer 指向 [ 符号的前一个符号,再从 run 的循环加一后,就可以指向 [ 符号。
}
[[nodiscard]] std::tuple<std::string, std::string> CodeReader::read() noexcept {
std::string codes = read_codes();
std::cin.get(); // 忽略空格
std::string inputs = read_inputs();
return {codes, inputs};
}
[[nodiscard]] std::string CodeReader::read_codes() noexcept {
static std::string valid = "<>+-,.[]";
std::string res;
char ch = std::cin.get();
while (ch != '$') {
if (valid.contains(ch)) { // C++23 特性,可替换为:valid.find(ch) != std::string::npos
res += ch;
}
ch = std::cin.get();
}
return res;
}
[[nodiscard]] std::string CodeReader::read_inputs() noexcept {
std::string res;
char ch = std::cin.get();
while (ch != '$') {
res += ch;
ch = std::cin.get();
}
return res.substr(0, res.size() - 1); // 忽略末尾空格
}
传统 OI 代码
C++#include <iostream>
#include <string>
using namespace std;
const int MEM_MAX = 30005;
string codes, inputs;
char memory[MEM_MAX]; // 内存
string::iterator codes_ptr, inputs_ptr;
int memory_ptr = 1;
void read() {
char ch = cin.get();
while (ch != '$') {
if (ch == '+' || ch == '-' || ch == '<' || ch == '>' || ch == '.' || ch == ',' || ch == '[' || ch == ']') {
codes += ch;
}
ch = cin.get();
}
cin.get();
ch = cin.get();
while (ch != '$') {
inputs += ch;
ch = cin.get();
}
// 初始化
inputs = inputs.substr(0, inputs.size() - 1);
codes_ptr = codes.begin();
inputs_ptr = inputs.begin();
}
void run() {
while (codes_ptr != codes.end()) {
char code = *codes_ptr;
switch (code) {
case '<':
memory_ptr--;
break;
case '>':
memory_ptr++;
break;
case '+':
memory[memory_ptr]++;
break;
case '-':
memory[memory_ptr]--;
break;
case '.':
cout.put(memory[memory_ptr]);
break;
case ',':
if (inputs_ptr == inputs.end()) {
memory[memory_ptr] = -1;
} else {
memory[memory_ptr] = *inputs_ptr;
inputs_ptr++;
}
break;
case '[':
if (memory[memory_ptr] == 0) {
int count = 1;
codes_ptr++;
char ch = *codes_ptr;
while (count != 0) {
if (ch == '[') {
count++;
} else if (ch == ']') {
count--;
}
codes_ptr++;
ch = *codes_ptr;
}
codes_ptr--; // 此时 pointer 指向 ] 所在位置,由循环末尾加一后,指向 ] 的下一个位置
}
break;
case ']':
int count = 1;
codes_ptr--;
char ch = *codes_ptr;
while (count != 0) {
if (ch == ']') {
count++;
} else if (ch == '[') {
count--;
}
codes_ptr--;
ch = *codes_ptr;
}
// 此时 pointer 指向 [ 的前一个位置,由循环末尾加一后,指向 [ 的位置。
break;
}
codes_ptr++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
read();
run();
return 0;
}