fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int t;
  9. cin >> t;
  10.  
  11. while (t--) {
  12. int n;
  13. string s;
  14. cin >> n >> s;
  15.  
  16. int x = 0, y = 0;
  17. for (char move : s) {
  18. if (move == 'U') y++;
  19. else if (move == 'D') y--;
  20. else if (move == 'L') x--;
  21. else x++;
  22. }
  23.  
  24. if ((abs(x) == 2 && y == 0) || (abs(y) == 2 && x == 0)) cout << "YES\n";
  25. else cout << "NO\n";
  26. }
  27. }
  28.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
NO
NO