B. Following Directions
Problem
Alperen is standing at the point (0,0)(0,0)(0,0). He is given a string sss of length nnn and performs nnn moves. The iii-th move is as follows:
- if si=Ls_i = \texttt{L}si=L, then move one unit left;
- if si=Rs_i = \texttt{R}si=R, then move one unit right;
- if si=Us_i = \texttt{U}si=U, then move one unit up;
- if si=Ds_i = \texttt{D}si=D, then move one unit down.
There is a candy at (1,1)(1,1)(1,1) (that is, one unit above and one unit to the right of Alperen’s starting point). You need to determine if Alperen ever passes the candy.
Alperen’s path in the first test case.
Input
The first line of the input contains an integer ttt (1≤t≤10001 \leq t \leq 10001≤t≤1000) — the number of testcases.
The first line of each test case contains an integer nnn (1≤n≤501 \leq n \leq 501≤n≤50) — the length of the string.
The second line of each test case contains a string sss of length nnn consisting of characters L\texttt{L}L, R\texttt{R}R, D\texttt{D}D, and U\texttt{U}U, denoting the moves Alperen makes.
Output
For each test case, output “YES” (without quotes) if Alperen passes the candy, and “NO” (without quotes) otherwise.
You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).
Example
Input
7
7
UUURDDL
2
UR
8
RRRUUDDD
3
LLL
4
DUUR
5
RUDLL
11
LLLLDDRUDRD
Output
YES
YES
NO
NO
YES
YES
NO
Note
In the first test case, Alperen follows the path
(0,0)→U(0,1)→U(0,2)→U(0,3)→R(1,3)→D(1,2)→D(1,1)→L(0,1).
(0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{U}}{\to} (0,2) \overset{\texttt{U}}{\to} (0,3) \overset{\texttt{R}}{\to} (1,3) \overset{\texttt{D}}{\to} (1,2) \overset{\texttt{D}}{\to} \color{green}{\mathbf{(1,1)}} \overset{\texttt{L}}{\to} (0,1).
(0,0)→U(0,1)→U(0,2)→U(0,3)→R(1,3)→D(1,2)→D(1,1)→L(0,1).
Note that Alperen doesn’t need to end at the candy’s location of (1,1)(1,1)(1,1), he just needs to pass it at some point.
In the second test case, Alperen follows the path
(0,0)→U(0,1)→R(1,1). (0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{R}}{\to} \color{green}{\mathbf{(1,1)}}. (0,0)→U(0,1)→R(1,1).
In the third test case, Alperen follows the path
(0,0)→R(1,0)→R(2,0)→R(3,0)→U(3,1)→U(3,2)→D(3,1)→D(3,0)→D(3,−1). (0,0) \overset{\texttt{R}}{\to} (1,0) \overset{\texttt{R}}{\to} (2,0) \overset{\texttt{R}}{\to} (3,0) \overset{\texttt{U}}{\to} (3,1) \overset{\texttt{U}}{\to} (3,2) \overset{\texttt{D}}{\to} (3,1) \overset{\texttt{D}}{\to} (3,0) \overset{\texttt{D}}{\to} (3,-1). (0,0)→R(1,0)→R(2,0)→R(3,0)→U(3,1)→U(3,2)→D(3,1)→D(3,0)→D(3,−1).
In the fourth test case, Alperen follows the path
(0,0)→L(−1,0)→L(−2,0)→L(−3,0). (0,0) \overset{\texttt{L}}{\to} (-1,0) \overset{\texttt{L}}{\to} (-2,0) \overset{\texttt{L}}{\to} (-3,0). (0,0)→L(−1,0)→L(−2,0)→L(−3,0).
Code
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
ll n;
cin>>n;
string s;
cin>>s;
ll x=0,y=0;
for(auto &t:s)
{
if(t=='L') x--;
if(t=='R') x++;
if(t=='U') y++;
if(t=='D') y--;
if(x==1&&y==1)
{
cout<<"YES"<<endl;
return;
}
}
cout<<"NO"<<endl;
}
int main()
{
ll t;
cin>>t;
while(t--) solve();
return 0;
}