这个方法很巧妙,可以自己试着debug一下就能懂的。
import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);while(sc.hasNext()) {
int n = sc.nextInt();for (int i = 0; i < n; i++) {
int x = sc.nextInt();int y = sc.nextInt();String s = sc.next();char[] str = s.toCharArray();int r = 0 , u = 0, l = 0, d = 0;for (int j = 0; j < s.length(); j++) {
//找出每串字符串里面的字符各几何if(str[j] == 'R') r++;else if(str[j] == 'L') l++;else if(str[j] == 'U') u++;else d++;}int f = 0;if(x >= 0 && r >= x) f++;else if(x < 0 && l >= -x) f++;else f = 0;if(y >= 0 && u >= y) f++;else if(y < 0 && d >= -y) f++;else f = 0;if(f == 2) System.out.println("YES");else System.out.println("NO");}}}
}