From 2e68eed36cfd83e119ea9b7349f719b4b06d383f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 27 Apr 2022 18:32:16 +0800 Subject: [PATCH] =?UTF-8?q?SXLK2022A.=20=E9=A2=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/ccf/record/62691baa9f31360077e283b7 --- Hydro/ccf/SXLK2022A/SXLK2022A.cpp | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Hydro/ccf/SXLK2022A/SXLK2022A.cpp diff --git a/Hydro/ccf/SXLK2022A/SXLK2022A.cpp b/Hydro/ccf/SXLK2022A/SXLK2022A.cpp new file mode 100644 index 00000000..34a67e77 --- /dev/null +++ b/Hydro/ccf/SXLK2022A/SXLK2022A.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105; + +int n; +// Raw strings +std::string s[N]; +// Defines: > +std::unordered_map> def; + +std::string dfs(std::string s) { + std::string r; + + for (int i = 0, j; i < s.size(); i += j) { + for (j = 0; i + j < s.size() && + ('0' <= s[i + j] && s[i + j] <= '9' || + 'a' <= s[i + j] && s[i + j] <= 'z' || + 'A' <= s[i + j] && s[i + j] <= 'Z' || s[i + j] == '_'); + j++) + ; + if (j) { + std::string tmp = s.substr(i, j), tmp2; + + if (def.count(tmp) && !def[tmp].second) { + def[tmp].second = true; + r += dfs(def[tmp].first); + def[tmp].second = false; + } else { + r += tmp; + } + } else { + r += s[i++]; + } + } + + return r; +} + +int main() { + freopen("preprocessor.in", "r", stdin); + freopen("preprocessor.out", "w", stdout); + + cin >> n; + for (int i = 0; i <= n; i++) { + std::getline(cin, s[i]); + } + + for (int i = 1; i <= n; i++) { + if (s[i][0] == '#') { // 预处理命令 + if (s[i].substr(1, 6) == "define") { + int p = s[i].find_first_of(' ', 8); + std::string name = s[i].substr(8, p - 8), + content = s[i].substr(p + 1); + def[name] = std::make_pair(content, false); + } else { // s[i].substr(1, 6) == "undef" + std::string name = s[i].substr(7); + def.erase(name); + } + cout << endl; + } else { // 普通文本 + cout << dfs(s[i]) << endl; + } + } + + return 0; +}