题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

#include <iostream>
#include <string>
#include <cctype>
#include <array>
#include <unordered_set>
using namespace std;

unordered_set<char> smap{'A', 'D', 'S', 'W'};

int regrex_search(string s, int& start, int& step) {
    if (s[start - 1] == ';' && smap.count(s[start])) {
        int i = start + 1;
        if (isdigit(s[i])) {
            if (s[i + 1] == '\0' || s[i + 1] == ';') {
                step = stoi(s.substr(i, 1));
                return 1;
            }
            if (isdigit(s[i + 1]) && (s[i + 2] == ';' || s[i + 2] == '\0')) {
                step = stoi(s.substr(i, 2));
                return 2;
            }
        }
    }
    return 0;
}

int main() {
    string ss;
    cin >> ss;
    if (ss == "") {
        return 0;
    }
    ss = ';' + ss;
    array<int, 2> idx = {0, 0};
    for (int i = 0; i < ss.length(); ++i) {
        int step = 0;
        int id_i = regrex_search(ss, i, step);
        if (id_i) {
            switch (ss[i]) {
                case 'A':
                    idx[0] -= step;
                    break;
                case 'D':
                    idx[0] += step;
                    break;
                case 'S':
                    idx[1] -= step;
                    break;
                case 'W':
                    idx[1] += step;
                    break;
            }
            i += id_i;
        }
    }
    cout << idx[0] << "," << idx[1] << endl;
}

全部评论

相关推荐

待现的未见之事:起码第一句要把自己的优势说出来吧。比如什么xx本27届学生,随时到岗....
点赞 评论 收藏
分享
吴offer选手:我卡在笔试才是最好笑的,甚至没给我发过笔试链接
投递哔哩哔哩等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务