#include <bits/stdc++.h>
using namespace std;
int n, idx;
string code[110];
map <string, string> mp;
map <string, int> qwq;
map <string, int> iszk;
string dy[110];
bool check(char c) {
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') return 1;
else return 0;
}
string solve(string &x) {
for (int i = 1; i <= idx; i ++) {
if (iszk[dy[i]]) continue;
if (dy[i] == " ") continue;
int t = 0;
while (1) {
t = x.find(dy[i], t);
// cout << x << " | " << dy[i] << " | " << t << "\n";
if (t == -1) break;
else {
if (check(x[t - 1]) || check(x[t + dy[i].size()])) {
++ t;
continue;
}
iszk[dy[i]] = 1;
string y = "";
for (int j = 0; j < t; j ++) {
y.push_back(x[j]);
}
string yy = mp[dy[i]];
for (int j = 0; j < yy.size(); j ++) {
y.push_back(yy[j]);
}
for (int j = t + dy[i].size(); j < x.size(); j ++) {
y.push_back(x[j]);
}
x = y;
solve(x);
iszk[dy[i]] = 0;
++ t;
}
}
}
return x;
}
int main() {
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen (".in", "r", stdin);
//freopen (".out", "w", stdout);
cin >> n;
string qwqawa;
getline(cin, qwqawa);
for (int i = 1; i <= n; i ++) {
getline(cin, code[i]);
}
for (int i = 1; i <= n; i ++) {
if (code[i][0] == '#') {
cout << "\n";
string op;
int j;
for (j = 1; j < code[i].size(); j ++) {
if (code[i][j] == ' ') {
j ++;
break;
}
op.push_back(code[i][j]);
}
if (op == "define") {
string x, y;
for (j; j < code[i].size(); j ++) {
if (code[i][j] == ' ') {
j ++;
break;
}
x.push_back(code[i][j]);
}
for (j; j < code[i].size(); j ++) {
y.push_back(code[i][j]);
}
mp[x] = y;
dy[++ idx] = x;
qwq[x] = idx;
} else {
string x;
for (j; j < code[i].size(); j ++) {
x.push_back(code[i][j]);
}
mp[x] = " ";
dy[qwq[x]] = " ";
qwq[x] = 0;
}
} else {
string zk;
for (int j = 0; j < code[i].size(); j ++) {
if (code[i][j] == ' ') {
// solve(zk);
// cout << "\n\n";
cout << solve(zk);
iszk.clear();
zk = "";
cout << " ";
} else zk.push_back(code[i][j]);
}
cout << solve(zk);
iszk.clear();
zk = "";
cout << "\n";
}
}
return 0;
}
我知道为什么错,应为不能处理递归。
但不知道怎么改AwA