#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
int n;
const long long mod = 1000000007;
unordered_map<string,pair<int,vector<string>>> m;
unordered_map<string, bool> simple;
unordered_map<string, string> res;//放置opt为1的静态的结果
//返回c得最终字符串
string get_now(string c) {
    string result;
    //说明为最简单的表达式
    if (simple[c]) {
        for (string s : m[c].second) {
            result += s;
        }
        return result;
    }
    //不是最简单的表达式
    for (string s : m[c].second) {
        if (s[0] != '$') {
            result += s;
        }
        else {
            string s_split = string(s.begin() + 1,s.end());
            if (m.find(s_split) == m.end())continue;
            //当opt为1的时候之前一定被求过
            if (m[s_split].first == 1) result += res[s_split];
            //当opt为2的时候需要动态递归求解
            else result += get_now(s_split);
        }
    }
    return result;
}

int main() {
    cin >> n;
    cin.ignore();
    while (n--) {
        string s;
        getline(cin, s);
        istringstream iss(s);
        int opt;
        iss >> opt;
        if (opt == 1) {
            string a;
            iss >> a;
            vector<string> b;
            string temp;
            bool flag = true;
            while (iss >>temp) {
                b.push_back(temp);
                if (temp[0] == '$') flag = false;
            }
            m[a] = { opt,b };
            simple[a] = flag;
            res[a] = get_now(a);
        }
        else if (opt == 2) {
            string a;
            iss >> a;
            vector<string> b;
            string temp;
            bool flag = true;
            while (iss >> temp) {
                b.push_back(temp);
                if (temp[0] == '$') flag = false;
            }
            m[a] = { opt,b };
            simple[a] = flag;
        }
        else {
            string c;
            iss >> c;
            if (m.find(c) == m.end()){ cout << "0" << endl; continue;
            }
            if (m[c].first == 1) cout << (res[c].size()%mod) << endl;
            else cout << (get_now(c).size() % mod) << endl;
        }
    }
    return 0;
}

大模拟题真的好恶心啊

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐