Directions#11765

Directions (Editorial)

Problem Author:

Ivan Balashov, Kostiantyn Denysov

Prepared by:

Kostiantyn Denysov

Editorial by:

Kostiantyn Denysov

We can independently check whether it is possible to make the -coordinate equal to and whether it is possible to make the -coordinate equal to .

To check if it is possible to make the -coordinate equal to , we calculate the total number of letters and in the given string. Then, we brute force all possible counts of in the final string (denote this count as c_E) and determine the count of as c_W. Finally, we check if the following equation holds for the -coordinate:

l_E \cdot c_E - l_W \cdot c_W = 0

Time complexity:

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = -1, inf = 1000111222;

inline bool good (int n, int a, int b) {
    for (int i = 0; i <= n; i++) {
        ll now = i * 1ll * a - (n - i) * 1ll * b;
        if (now == 0) {
            return true;
        }
    }
    return false;
}

inline void test_case () {
    int n, N, S, E, W;
    cin >> n >> E >> W >> N >> S;
    int cnt[2] = {};
    string s;
    cin >> s;
    for (char c : s) {
        if (c == 'N' || c == 'S') {
            ++cnt[0];
        }
        else {
            ++cnt[1];
        }
    }
    if (good(cnt[0], N, S) && good(cnt[1], E, W)) {
        cout << "Yes\n";
    }
    else {
        cout << "No\n";
    }
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
    cin >> t;
    for (int test = 1; test <= t; test++) {
        test_case();
    }
}