fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <bits/stdc++.h>
  6. #define ll long long
  7. using namespace std;
  8. int main() {
  9. int t; cin>>t;
  10. while (t--) {
  11. int n; cin>>n;
  12. string str; cin>>str;
  13. bool Alice = 1; bool go = 1;
  14. for (int i = 0; i<n; i++) {
  15. if (n==1) {
  16. if (str[0]=='U') {
  17. Alice = !Alice ;
  18. go = 0;
  19. break;
  20. }
  21. else {
  22. go = 0;
  23. break;
  24. }
  25. }
  26. if (n==2) {
  27. if (str[0]==str[1]) {
  28. go = 0;
  29. break;
  30. }
  31. else {
  32. Alice=!Alice;
  33. go = 0;
  34. break;
  35. }
  36. }
  37. if (str[i]=='U') {
  38. Alice=!Alice;
  39. if (i==0) {
  40. if (str[i+1]=='U'){str[i+1]='D';}
  41. else if (str[i+1]=='D'){str[i+1]='U';}
  42. if (str[n-1]=='U'){str[n-1]='D';}
  43. else if (str[n-1]=='D'){str[n-1]='U';}
  44. }
  45. else if (i==n-1) {
  46. if (str[0]=='U'){str[0]='D';}
  47. else if (str[0]=='D'){str[0]='U';}
  48. if (str[i-1]=='U'){str[i-1]='D';}
  49. else if (str[i-1]=='D'){str[i-1]='U';}
  50. }
  51. for (int j = i; j<n-1; j++) {
  52. swap(str[j],str[j+1]);
  53. }
  54. str.pop_back(); n--; i=0;
  55. }
  56.  
  57. }
  58. if (Alice) {
  59. cout<<"NO"<<'\n';
  60. }
  61. else {
  62. cout<<"YES"<<'\n';
  63. }
  64. }
  65. }
  66.  
  67.  
Success #stdin #stdout 0.01s 5320KB
stdin
3
5
UUDUD
5
UDDUD
2
UU
stdout
YES
NO
NO