真的建议学一下C++的正则表达式,比一个字符一个字符模拟好写多了,还不容易码错。

本题共三步:

第一步,我们按照分号对指令串进行分词,转为单个指令。

第二步,我们检测单个指令的合法性。

第三步,对于合法指令,按照题意模拟即可。

使用到的正则表达式库内容:

std::regex,正则表达式对象。

std::sregex_token_iterator,用来对指令串进行分词操作。

std::regex_match,对字符串进行匹配。

std::smatch,存储regex_match匹配的结果。match[0]是原串,match[1..]开始是我们在正则表达式中的分组的匹配结果。

然后就做完了~细节可看代码。

#include <bits/stdc++.h>

using namespace std;

void solve() {
    std::string s;
    cin >> s;
    std::regex pattern1(";"), pattern2("([WASD])(\\d+)");
    std::sregex_token_iterator pos(s.begin(), s.end(), pattern1, -1);
    int x = 0, y = 0;
    for (decltype(pos) end; pos != end; ++pos) {
        auto subs = pos->str();
        smatch match;
        auto ret = regex_match(subs, match, pattern2);
        if (!ret) continue;
        int d = stoi(match[2].str());
        if (subs[0] == 'W') y += d;
        else if (subs[0] == 'S') y -= d;
        else if (subs[0] == 'A') x -= d;
        else x += d;
    }
    cout << x << "," << y;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}