【奇文共赏4.0】 啸皱痨屎爆草2023 CSP-S!
有什么最新消息或者缺失欢迎私信,我尽力更新
可能夹带私货QAQ
\colorbox{skyblue}{\color{purple}\large\text{【上集回顾】}} 周鲁冥师押题2023 CSP-J,百发百中!
很抱歉这篇专栏晚了整整
感谢@大眼仔Happy 的贡献。(←关注那个大好人)要不是他我都不知道啸皱牢逝更新了。
\colorbox{yellow}{\color{red}\huge\textbf{【本期看点】}}\newline\color{blue}\large\textbf{周鲁冥师详解}\color{green}\huge\textbf{CSP-S!}
销轴老石不仅给我们带来学长的馈赠,还教导我们一定要赞美
小周老师的代码真的可以 AC 哟!
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct Variable{
string name;
string type;
long long pos;
};
bool operator < (Variable a, Variable b) {
return a.pos < b.pos;
}
Variable CurVar(long long pos) {
Variable res;
res.pos = pos;
return res;
}
struct Type{
long long daxiao;
int duiqi;
//empty vars means that it is a basic type
vector<Variable> vars;
map<string, long long> varPos;
};
map<string, Type> _types;
Type _main;
//map<string, long long> _varPos;
//vector<Variable> _theMain;
//long long _curPos = 0;
int _ops;
long long UpperMultiple(long long ori, int base) {
return (ori+base-1)/base*base;
}
Type BasicType(int bytes) {
Type res;
res.daxiao = bytes;
res.duiqi = bytes;
return res;
}
void Init() {
_types["byte"] = BasicType(1);
_types["short"] = BasicType(2);
_types["int"] = BasicType(4);
_types["long"] = BasicType(8);
}
Variable NewVar(string type, string name, long long pos) {
Variable res;
res.type = type;
res.name = name;
res.pos = pos;
return res;
}
void GenBasicType(string name, int bytes) {
_types[name] = BasicType(bytes);
}
Type NewType(vector<string>& types, vector<string>& names) {
Type res;
Type* curType;
res.daxiao = 0;
res.duiqi = 0;
for (int i = 0; i < types.size(); i++) {
curType = &(_types[types[i]]);
res.duiqi = max(res.duiqi, curType->duiqi);
res.daxiao = UpperMultiple(res.daxiao, curType->duiqi);
res.varPos[names[i]] = res.vars.size();
res.vars.push_back(NewVar(types[i], names[i], res.daxiao));
res.daxiao += curType->daxiao;
// cout << res.daxiao << ',' << names[i] << endl;
}
res.daxiao = UpperMultiple(res.daxiao, res.duiqi);
return res;
}
void DefineType() {
int k;
string s;
Type* newType;
vector<string> t;
vector<string> n;
cin >> s >> k;
t.resize(k);
n.resize(k);
for (int i = 0; i < k; i++) {
cin >> t[i] >> n[i];
}
_types[s] = NewType(t, n);
newType = &_types[s];
cout << newType->daxiao << ' ' << newType->duiqi << endl;
// cout << _types[s].daxiao << ','
// << _types[s].duiqi << ',' << s << endl;
}
void DefineVar() {
string t;
string n;
Type* curType;
cin >> t >> n;
curType = &(_types[t]);
_main.daxiao = UpperMultiple(_main.daxiao, curType->duiqi);
_main.varPos[n] = _main.vars.size();
_main.vars.push_back(NewVar(t, n, _main.daxiao));
cout << _main.daxiao << endl;
_main.daxiao += curType->daxiao;
}
void QueryVar() {
string s;
cin >> s;
string cur;
char solo;
Type* curType = &_main;
long long res = 0;
for (int i = 0; i < s.length(); i++) {
solo = s[i];
if (solo == '.') {
res += curType->vars[curType->varPos[cur]].pos;
curType = &_types[curType->vars[curType->varPos[cur]].type];
cur = "";
continue;
}
cur += solo;
}
res += curType->vars[curType->varPos[cur]].pos;
curType = &_types[curType->vars[curType->varPos[cur]].type];
cout << res << endl;
}
vector<string> resList;
bool FindVar(Type& type, long long pos) {
// cout << pos << "I love CCF" << endl;
// cout << "I love CCF" << endl;
if (pos >= type.daxiao) {
return false;
}
// cout << "I love CCF" << endl;
if (type.vars.empty()) {
return true;
}
// cout << "I love CCF" << endl;
int varPos = upper_bound(type.vars.begin(),
type.vars.end(), CurVar(pos))-type.vars.begin()-1;
// cout << "you" << endl;
// cout << varPos << ',' << pos << ',' << type.vars.size() << endl;
resList.push_back(type.vars[varPos].name);
// cout << "you" << endl;
return FindVar(_types[type.vars[varPos].type], pos - type.vars[varPos].pos);
// return false;
}
void QueryPos() {
long long addr;
resList.clear();
cin >> addr;
if (FindVar(_main, addr)) {
for (int i = 0; i < resList.size()-1; i++) {
cout << resList[i] << '.';
}
cout << resList[resList.size()-1] << endl;
return;
}
cout << "ERR" << endl;
}
void Query() {
int op;
cin >> op;
switch(op) {
case 1:
DefineType();
break;
case 2:
DefineVar();
break;
case 3:
QueryVar();
break;
default:
QueryPos();
}
}
int main() {
ios::sync_with_stdio(false);cin.tie();cout.tie();
Init();
cin >> _ops;
while (_ops--) {
Query();
}
return 0;
}
//甚至还符合周礼
我实在想不出还能整什么活了
upd:小周老师大概是真的似了罢。