ccfcsp 37-3 模板展开 80分

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值