fork download
  1. //#define DEBUG //comment when you have to disable all debug macros.
  2. //#define LOCAL //uncomment for testing from local file
  3. #define NDEBUG //comment when all assert statements have to be enabled.
  4. #include <iostream>
  5. #include <cstring>
  6. #include <sstream>
  7. #include <fstream>
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <cmath>
  11. #include <vector>
  12. #include <set>
  13. #include <map>
  14. #include <bitset>
  15. #include <climits>
  16. #include <ctime>
  17. #include <algorithm>
  18. #include <functional>
  19. #include <stack>
  20. #include <queue>
  21. #include <list>
  22. #include <deque>
  23. #include <sys/time.h>
  24. #include <iomanip>
  25. #include <cstdarg>
  26. #include <utility> //std::pair
  27. #include <cassert>
  28.  
  29. #define fd(i,a) for(i=1;i<=a;i++)
  30. #define fa(i,a,b) for(i=a;i<=b;i++)
  31. #define fs(i,a,b,c) for(i=a;i<=b;i+=c)
  32. #define tr(c,i) for(typeof(c.begin()) i = (c).begin(); i != (c).end(); i++)
  33. #define present(c,x) ((c).find(x) != (c).end())
  34. #define all(x) x.begin(), x.end()
  35. #define clr(x,y) memset(x,y,sizeof x)
  36. #define log2(x) (log(x)/log(2))
  37. #define ARRAY_SIZE(arr) (1[&arr]-arr)
  38. #define INDEX(arr,elem) (lower_bound(all(arr),elem)-arr.begin())
  39. #define equals(a,b) (a.compareTo(b)==0) //for strings only
  40. template<class P, class Q> inline void smin(P &a, Q b) { if (b < a) a = b; }
  41. template<class P, class Q> inline void smax(P &a, Q b) { if (a < b) a = b; }
  42.  
  43. #define pb push_back
  44. #define mp make_pair
  45. #define lld long long int
  46. #define MOD 1000000007
  47. #define gcd __gcd
  48.  
  49. using namespace std;
  50.  
  51. #ifdef DEBUG
  52. #define debug(args...) {dbg,args; cerr<<endl;}
  53. #else
  54. #define debug(args...) // Just strip off all debug tokens
  55. #endif
  56.  
  57. struct debugger
  58. {
  59. template<typename T> debugger& operator , (const T& v)
  60. {
  61. cerr<<v<<" ";
  62. return *this;
  63. }
  64.  
  65. }dbg;
  66.  
  67. template<class T>
  68. inline void inputInt(T &n )
  69. {
  70. n=0;
  71.  
  72. T ch=getchar_unlocked();
  73. /*int sign=1;
  74.   while(( ch < '0' || ch > '9') && ch!='-')
  75.   ch=getchar_unlocked();
  76.   if(ch=='-')
  77.   {
  78.   sign=-1;
  79.   ch=getchar_unlocked();
  80.   }
  81.   while( ch >= '0' && ch <= '9' )
  82.   n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
  83.   n*=sign;*/
  84. while( ch < '0' || ch > '9')
  85. ch=getchar_unlocked();
  86. while( ch >= '0' && ch <= '9' )
  87. n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
  88. }
  89.  
  90. inline void inputStr(string &str){
  91. str="";
  92. char ch = getchar_unlocked();
  93. debug(ch);
  94. int i=0;
  95. while(ch<33){ch=getchar_unlocked();}
  96. while(ch!='\n'){str+=ch;ch=getchar_unlocked();}
  97. }
  98.  
  99.  
  100. int main()
  101. {
  102.  
  103. int T,n,m;
  104. string s;
  105. inputInt(T);
  106. while(T--){
  107. inputInt(n);
  108. inputInt(m);
  109. cin.clear();
  110. cin>>s;
  111. int len = s.length(), L,U,R,D, h=0, v=0;
  112. //debug("yo",s);
  113. bool safe=true;
  114. L=U=R=D=0;
  115. for(int i=0;i<len;i++)
  116. {
  117. if(s[i]=='L')h--;
  118. else if(s[i]=='R')h++;
  119. else if(s[i]=='U')v++;
  120. else v--;
  121. if(h<0 && abs(h)>L)L=abs(h);
  122. else if(h>0 && abs(h)>R)R=abs(h);
  123. if(v<0 && abs(v)>D)D=abs(v);
  124. else if(v>0 && abs(v)>U)U=abs(v);
  125. //debug(h,v,L,R,U,D,m,n);
  126. if(L+R>=m || U+D>=n){
  127. safe=false;
  128. break;
  129. }
  130. }
  131. printf("%s\n",safe?"safe":"unsafe");
  132. }
  133. }
Success #stdin #stdout 0s 15240KB
stdin
5
1 1
R
2 3
LLRU
3 2
LLRU
4 3
ULURUDRDLD
3 6
RURUR
stdout
unsafe
safe
unsafe
safe
safe