B. 移动撤销

栈的操作,当进栈时 而且 栈不为空时,就将栈顶的元素弹出,之后就模拟栈里的元素即可。

#include<bits/stdc++.h>
using namespace std;

string s;
int n;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int main()
{
    IOS;
    cin >> n;
    cin >> s;
    int x = 0, y = 0;
    stack < char > s1;
    for(auto i : s){
        if(i != 'Z') s1.push(i);
        else if(i == 'Z' && s1.empty()) continue;
        else s1.pop();
    }
    while(!s1.empty()){
        char i = s1.top();
        s1.pop();
        if(i == 'W') y += 1;
        if(i == 'A') x -= 1;
        if(i == 'S') y -= 1;
        if(i == 'D') x += 1;
    }
    cout << x << " " << y << endl;
}