#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
map<string, long long int> zhi; // 直接赋值的变量存储其值的长度
map<string, vector<string>> jian; // 间接赋值的变量存储其表达式
bool isvalid(const string &s) {
if (s.empty()) return false;
for (char c : s) {
if (!(islower(c) || isdigit(c))) return false;
}
return true;
}
long long int jiancnt(const string &var) {
long long int cnt = 0;
for (const string &op : jian[var]) {
if (op[0] != '$') {
cnt += op.size();
} else {
string var_name = op.substr(1);
if (zhi.count(var_name)) {
cnt += zhi[var_name];
} else if (jian.count(var_name)) {
cnt += jiancnt(var_name);
}
}
cnt %= M;
}
return cnt % M;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
cin.ignore();
for (int i = 0; i < n; ++i) {
string line;
getline(cin, line);
stringstream ss(line);
vector<string> words;
string word;
while (ss >> word) {
words.push_back(word);
}
if (words.empty()) continue;
if (words[0] == "1") {
if (words.size() < 3 || !isvalid(words[1])) continue;
string var = words[1];
long long cnt = 0;
for (size_t j = 2; j < words.size(); ++j) {
if (words[j][0] != '$') {
cnt += words[j].size();
} else {
string v = words[j].substr(1);
if (zhi.count(v)) {
cnt += zhi[v];
} else if (jian.count(v)) {
cnt += jiancnt(v);
}
}
cnt %= M;
}
zhi[var] = cnt % M;
jian.erase(var);
} else if (words[0] == "2") {
if (words.size() < 3 || !isvalid(words[1])) continue;
string var = words[1];
jian[var].clear();
for (size_t j = 2; j < words.size(); ++j) {
jian[var].push_back(words[j]);
}
zhi.erase(var);
} else if (words[0] == "3") {
if (words.size() < 2 || !isvalid(words[1])) {
cout << 0 << endl;
continue;
}
string var = words[1];
if (zhi.count(var)) {
cout << zhi[var] % M << endl;
} else if (jian.count(var)) {
cout << jiancnt(var) % M << endl;
} else {
cout << 0 << endl;
}
}
}
return 0;
}
ccfcsp 37-3 模板展开 80分
最新推荐文章于 2025-12-03 14:22:43 发布
1435

被折叠的 条评论
为什么被折叠?



