P9754 [CSP-S 2023] 结构体 题解
Untitled10032 · · 题解
题意
模拟 C++ 变量与结构体对于内存的使用。支持操作:
- 新建结构体。
- 为结构体或变量申请内存。
- 按名称访问对应内存地址。
- 按内存地址查找对应名称。
要求数据对齐,详见题面。
思路
做大模拟还是得有一点面向对象的思想的。可以将题目中的数据结构封装成一个类。
内存和结构体中数据类型的嵌套关系可以抽象地看成一个 DAG(有向无环图)。若类型 B 是类型 A 的一个成员,那么可以看做 A 有一条连向 B 的边。
除了类型名,在结构体或内存中,每个变量都有自己的变量名,这个变量名是和边一起存储的,为了处理查找操作,每条边还需要存一个偏移量,表示从此结构体起始地址偏移多少到这个变量。
struct Struct {
struct Edge {
Struct *to;
string name;
int delta; //从此结构体起始地址偏移多少到这个变量
};
vector<Edge> members;
int size;
int order; //对齐要求,其实叫 align 更好,但是考场上是这么写的
} BYTE, SHORT, INT, LONG, memory;
虽然这个结构体的名字叫结构体,但是其实它表示的不止是结构体,而是类似更广泛的概念:数据结构。(但 structure 太长了,考场上不想用它做名字。)
-
INT、BYTE等基本类型是数据结构:它们没有出边(即没有成员变量),对齐要求等于题目给出的分别的大小。 -
本题中内存可以看做是一个数据结构:因为不涉及到内存的释放、动态分配等,只是从前往后一次分配,所以在内存中申请变量的操作和在结构体中声明成员变量的操作是几乎一致的,可以统一处理。
于是此题就变成了类似这样的递归查找问题。只要从表示内存的数据结构开始,沿着给出的数或字符串依次往下找即可。找到头之后记得判断一下是否 ERR。
关于内存对齐,依照题意模拟即可,可以考虑以下函数:
inline int calc(const int size, const int order) {
if (size % order == 0)
return size;
return (size / order + 1) * order;
}
在一个数据结构的成员都添加完之后用对齐要求更新数据结构占空间大小。
代码
struct Struct:表示数据结构的结构体。map<string, Struct *> names:用于存储结构体名的平衡树,方便申请变量或定义结构体。inline void init():对基本类型进行初始化。inline int calc(const int size, const int order):输入未考虑对齐的数据结构大小和对齐要求,返回对齐后的数据结构大小。inline void inputStruct():输入(定义)结构体。inline int getAddr():给出名称获取相应地址。inline void findVar():给出地址查找相应名称。
#include <iostream>
#include <string>
#include <vector>
#include <map>
#define int long long
using namespace std;
struct Struct {
struct Edge {
Struct *to;
string name;
int delta; //从此结构体起始地址偏移多少到这个变量
};
vector<Edge> members;
int size;
int order; //dui qi yao qiu
} BYTE, SHORT, INT, LONG, memory;
map<string, Struct *> names;
inline void init() {
BYTE.size = 1;
SHORT.size = 2;
INT.size = 4;
LONG.size = 8;
BYTE.order = 1;
SHORT.order = 2;
INT.order = 4;
LONG.order = 8;
names.insert({ "byte", &BYTE });
names.insert({ "short", &SHORT });
names.insert({ "int", &INT });
names.insert({ "long", &LONG });
}
inline int calc(const int size, const int order) {
if (size % order == 0)
return size;
return (size / order + 1) * order;
}
inline void inputStruct() {
Struct *s = new Struct;
s->order = 1;
s->size = 0;
string name;
cin >> name;
names.insert({ name, s });
int k;
cin >> k;
while (k--) {
string type_str;
cin >> type_str >> name;
//cout << type_str << endl;
Struct *type = names.find(type_str)->second;
const int t = calc(s->size, type->order);
s->members.push_back({ type, name, t });
s->size = t + type->size;
s->order = max(s->order, type->order);
}
s->size = calc(s->size, s->order);
cout << s->size << ' ' << s->order << '\n';
}
inline int getAddr() {
string name;
cin >> name;
string::iterator it = name.begin();
Struct *type = &memory;
int sum = 0;
while (it != name.end()) {
string now;
for (; it != name.end() && *it != '.'; ++it)
now.push_back(*it);
if (it != name.end())
++it; //skip '.'
for (Struct::Edge &i : type->members) {
if (i.name == now) {
type = i.to;
sum += i.delta;
break;
}
}
}
return sum;
}
inline void findVar() {
int addr;
cin >> addr;
Struct *type = &memory;
if (memory.members.size() == 0) {
//memory 里啥都没有
cout << "ERR\n";
return;
}
string path = "";
while (true) {
if (type->members.size() == 0)
break;
for (vector<Struct::Edge>::iterator i = type->members.begin(); i != type->members.end(); ++i) {
if ((i + 1) == type->members.end() || i->delta <= addr && (i + 1)->delta > addr) {
if (i->delta + i->to->size <= addr) {
cout << "ERR\n";
return;
}
type = i->to;
addr -= i->delta;
path += i->name + '.';
break;
}
}
}
path.pop_back(); //erase '.' at the end
cout << path << '\n';
}
signed main() {
freopen("struct.in", "r", stdin);
freopen("struct.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
init();
while (n--) {
char opt;
cin >> opt;
//cout << n << endl;
if (opt == '1') {
inputStruct();
} else if (opt == '2') {
string type_str, name;
cin >> type_str >> name;
Struct *type = names.find(type_str)->second;
const int t = calc(memory.size, type->order);
memory.members.push_back({ type, name, t });
memory.size = t + type->size;
cout << t << '\n';
} else if (opt == '3') {
cout << getAddr() << '\n';
} else {
findVar();
}
}
return 0;
}