题解 | #坐标移动#

坐标移动

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

简单正则表达式匹配思路,容易理解。
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include<regex>
using namespace std;

int main(){
    string s;
    getline(cin,s);
    int x=0,y=0;
    int j=0;
    for(int i=0;i<s.size();++i)
    {
        if(s[i]==';')
        {
            string t=s.substr(j,i-j);
            regex pattern("^([ASWD]{1})(\\d{1,2})$");
            smatch result;
            bool flag=regex_match(t,result,pattern);
            if(flag)
            {
                if(result[1]=='A') x-=stoi(result[2]);
                else if(result[1]=='D') x+=stoi(result[2]);
                else if(result[1]=='S') y-=stoi(result[2]);
                else if(result[1]=='W') y+=stoi(result[2]);
            }
            while(i<s.size()&&s[i+1]==';') ++i;
            j=i+1;
        }
    }
    cout<<x<<','<<y<<endl;
}


全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
06-20 19:40
中原工学院 Java
网络存储:十几天不会让你拉人办卡就结束了吧?
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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