题面
大模拟
首先是数据类型
我分了以下几种(有nxt者均使用链表)
arr: 数组
expression: 表达式(_
为下标,f
为符号)
node: 引用(_
为下标)
sentence: 语句(list为参数列表)
然后读入直接分类型读入即可
解释直接上1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// #define DEBUG fprintf(stderr, "run at line %d, function %s\n", __LINE__, __FUNCTION__)
using namespace std;
char buf[1 << 20], *ST = buf, *EN = buf;
inline int GC() {
if(ST == EN) ST = buf, EN = buf + fread(buf, 1, 1 << 20, stdin);
if(ST == EN) return EOF;
return *(ST++);
return getchar();
}
int cntT = 0;
inline int get_c() {
char c = GC();
while(c == '#')
while(c != '\n') c = GC();
if(c == EOF) {
if(cntT == 0) cntT = 1;
else throw(__LINE__);
}
return c;
}
int bufintt[500010];
int cntintt;
struct arr {
int *p;
int start;
inline int& operator [] (int x) {DEBUG;/* cerr << x << endl; */return p[x - start];}
inline void init(int l, int r) {
start = l;
p = bufintt + cntintt;
cntintt += r - l + 1;
memset(p, 0, sizeof(int[r - l + 1]));
}
};
string bufstr[20000];
int cntstr;
map<string, int> bufint;
map<string, arr> bufarr;
struct expression {
int type;
int f;
string name;
expression *next, *_;
expression() {type = f = 0; next = _ = nullptr;}
inline int get() {
if(type == 0) {
return f + (next ? next -> get() : 0);
}
if(type == 1) {
return f * bufint[name] + (next ? next -> get() : 0);
}
if(type == 2) {
return f * bufarr[name][_ -> get()] + (next ? next -> get() : 0);
}
die("error type");
return 0;
}
};
struct node {
int type;
string name;
expression *_;
inline int& get() {
DEBUG;
// cerr << name << endl << type << endl;
if(type == 1) return bufint[name];
DEBUG;
// fprintf(stderr, "%p\n", _);
if(type == 2) return bufarr[name][_ -> get()];
die("error type");
return type;
}
node() {memset(this, 0, sizeof(*this));}
};
struct sentence {
sentence *nxt;
void *list[4];
int type;
sentence() {memset(this, 0, sizeof(*this));}
} *root, bufsen[20000];
int cntsen;
const int _vars = 0, _while = 1, _hor = 2, _ihu = 3, _set = 4, _yosoro = 5;
typedef int (*fcmp)(int, int);
void work(sentence*);
string name;
sentence *froot;
void fvars(void **list) {//list: 0 定义语句头
DEBUG;
froot = (sentence*)(list[0]);
while(froot != nullptr) {
list = froot -> list;
DEBUG;
int type = *((int*)(list[0]));
DEBUG;
name = *((string*)(list[1]));
DEBUG;
// cerr << name << endl;
// cerr << type << endl;
if(type == 1) bufint[name] = 0;
else bufarr[name].init(*(int*)(list[2]), *(int*)(list[3]));
DEBUG;
froot = froot -> nxt;
}
}
void fwhile(void **list) {//list: 0 cmp函数 1 第一个表达式 2 第二个表达式 3 包含的语句块头
fcmp cmp = (fcmp)(list[0]);
expression i = *(expression*)(list[1]), j = *(expression*)(list[2]);
while(cmp(i.get(), j.get())) work((sentence*)list[3]);
}
void fihu(void **list) {//同上
fcmp cmp = (fcmp)(list[0]);
expression i = *(expression*)(list[1]), j = *(expression*)(list[2]);
if(cmp(i.get(), j.get())) work((sentence*)list[3]);
}
void fhor(void **list) {//list: 0 变量 1 start 2 end 3 语句块头
node i = *(node*)(list[0]);
expression start = *(expression*)(list[1]), end = *(expression*)(list[2]);
for(i.get() = start.get(); i.get() <= end.get(); i.get()++) work((sentence*)(list[3]));
}
void fyosoro(void **list) { // list: 0 输出
int ans = ((expression*)(list[0])) -> get();
printf("%d ", ans);
}
void fset(void **list) {//list: 0 变量 1 值
node from = *(node*)(list[0]);
expression x = *(expression*)(list[1]);
DEBUG;
from.get() = x.get();
DEBUG;
}
void work(sentence* now) {
if(now == nullptr) return;
if(now -> type == _vars) fvars(now -> list);
else if(now -> type == _ihu) fihu(now -> list);
else if(now -> type == _hor) fhor(now -> list);
else if(now -> type == _set) fset(now -> list);
else if(now -> type == _yosoro) fyosoro(now -> list);
else if(now -> type == _while) fwhile(now -> list);
else die("The type of sentence isn't expected");
work(now -> nxt);
}
char nowc = get_c();
int readint() {
for(; !isdigit(nowc) && nowc != '-'; nowc = get_c());
int f = 1, a = 0;
if(nowc == '-') f = -1, nowc = get_c();
for(; isdigit(nowc); nowc = get_c()) a = a * 10 + nowc - 48;
return a * f;
}
string getstring() {
DEBUG;
string ans;
while(!isalnum(nowc)) nowc = get_c();
while(isalnum(nowc)) ans += nowc, nowc = get_c();
DEBUG;
return ans;
}
expression bufex[20000];
int cnt;
inline expression* readexpression() {
DEBUG;
while(nowc == ' ') nowc = get_c();
if(!isalnum(nowc) && nowc != '-' && nowc != '+') return nullptr;
DEBUG;
expression *ans = bufex + (cnt++);
ans -> f = 1;
DEBUG;
if(nowc == '+') nowc = get_c();
if(nowc == '-') nowc = get_c(), ans -> f = -1;
while(nowc == ' ') nowc = get_c();
if(!isalnum(nowc)) die("Except a var name");
DEBUG;
if(isalpha(nowc)) {
ans -> name = getstring();
while(nowc == ' ') nowc = get_c();
if(nowc == '[') nowc = get_c(), ans -> type = 2, ans -> _ = readexpression(), nowc = get_c();
else ans -> type = 1;
}
else ans -> f *= readint(), ans -> type = 0;
ans -> next = readexpression();
return ans;
}
node bufnd[20000];
int cntnd;
inline node* readnode() {
DEBUG;
while(nowc == ' ') nowc = get_c();
if(!isalpha(nowc)) die("Except a var name");
node *ans = bufnd + (cntnd++);
string lala = getstring();
ans -> name = lala;
DEBUG;
if(nowc == '[') nowc = get_c(), ans -> type = 2, ans -> _ = readexpression(), nowc = get_c();
else ans -> type = 1;
DEBUG;
return ans;
}
int lt(int a, int b) {
return a < b;
}
int gt(int a, int b) {
return a > b;
}
int le(int a, int b) {
return a <= b;
}
int ge(int a, int b) {
return a >= b;
}
int eq(int a, int b) {
return a == b;
}
int neq(int a, int b) {
return a != b;
}
int _1 = 1, _2 = 2;
inline sentence* readvars() {
DEBUG;
while(!isalpha(nowc) && nowc != '}') nowc = get_c();
if(nowc == '}') return nullptr;
sentence *ans = bufsen + (cntsen++);
string *s = bufstr + (cntstr++);
*s = getstring();
ans -> list[1] = s;
while(!isalpha(nowc)) nowc = get_c();
if(nowc == 'i') {
ans -> list[0] = &_1;
getstring();
}
else {
ans -> list[0] = &_2;
getstring();
getstring();
int *l = new int, *r = new int;
*l = readint(), *r = readint();
ans -> list[2] = l, ans -> list[3] = r;
while(nowc != ']') nowc = get_c();
}
ans -> nxt = readvars();
return ans;
}
inline sentence* read(char endc) {
DEBUG;
sentence* ans = bufsen + (cntsen++);
DEBUG;
while(nowc != ':' && nowc != '{' && nowc != endc) nowc = get_c();
if(nowc == endc) return nullptr;
if(nowc == ':') {
nowc = get_c();
string type = getstring();
if(type == "set") {
DEBUG;
ans -> type = _set;
ans -> list[0] = readnode();
DEBUG;
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[1] = readexpression();
}
else if(type == "yosoro") {
ans -> type = _yosoro;
ans -> list[0] = readexpression();
}
else die("command not found");
}
else {
nowc = get_c();
string type = getstring();
if(type == "ihu") {
ans -> type = _ihu;
string cmptype = getstring();
fcmp cmp;
if(cmptype == "lt") cmp = lt;
else if(cmptype == "gt") cmp = gt;
else if(cmptype == "le") cmp = le;
else if(cmptype == "ge") cmp = ge;
else if(cmptype == "eq") cmp = eq;
else if(cmptype == "neq") cmp = neq;
else die("cmp is undefined");
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[0] = (void*)cmp;
ans -> list[1] = readexpression();
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[2] = readexpression();
ans -> list[3] = read('}');
nowc = get_c();
}
else if(type == "while") {
ans -> type = _while;
string cmptype = getstring();
fcmp cmp;
if(cmptype == "lt") cmp = lt;
else if(cmptype == "gt") cmp = gt;
else if(cmptype == "le") cmp = le;
else if(cmptype == "ge") cmp = ge;
else if(cmptype == "eq") cmp = eq;
else if(cmptype == "neq") cmp = neq;
else die("cmp is undefined");
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[0] = (void*)cmp;
ans -> list[1] = readexpression();
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[2] = readexpression();
ans -> list[3] = read('}');
nowc = get_c();
}
else if(type == "hor") {
ans -> type = _hor;
ans -> list[0] = readnode();
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[1] = readexpression();
while(nowc != ',') nowc = get_c();
nowc = get_c();
ans -> list[2] = readexpression();
ans -> list[3] = read('}');
nowc = get_c();
}
else if(type == "vars") {
ans -> type = _vars;
ans -> list[0] = readvars();
nowc = get_c();
}
else die("command not found");
}
ans -> nxt = read(endc);
return ans;
}
int main() {
root = read(EOF);
DEBUG;
work(root);
puts("");
return 0;
}