fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. static inline bool is_return(char a, char b)
  8. {
  9. if (a == 'W' && b == 'E')
  10. {
  11. return true;
  12. }
  13. if (a == 'E' && b == 'W')
  14. {
  15. return true;
  16. }
  17. if (a == 'S' && b == 'N')
  18. {
  19. return true;
  20. }
  21. if (a == 'N' && b == 'S')
  22. {
  23. return true;
  24. }
  25. return false;
  26. }
  27.  
  28. static inline bool is_any_return(std::string s)
  29. {
  30. for (int i = 1; i < s.size(); i++)
  31. {
  32. if (is_return(s[i - 1], s[i]))
  33. {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39.  
  40. static inline bool is_loop(std::string s)
  41. {
  42. int x = 0, y = 0;
  43.  
  44. for (int i = 0; i < s.size(); i++)
  45. {
  46. char ch = s[i];
  47. switch (ch)
  48. {
  49. case 'W':
  50. x--;
  51. break;
  52. case 'S':
  53. y--;
  54. break;
  55. case 'E':
  56. x++;
  57. break;
  58. case 'N':
  59. y++;
  60. break;
  61. }
  62. }
  63.  
  64. return (x == 0) && (y == 0);
  65. }
  66.  
  67. int main()
  68. {
  69. ios_base::sync_with_stdio(false);
  70.  
  71. string s;
  72. int d;
  73. cin >> d;cin.get();
  74.  
  75. while (d--)
  76. {
  77. int x = 0, y = 0;
  78. getline(cin, s);
  79.  
  80. bool res = false;
  81.  
  82. for (int i = 0; i < s.size() && res == false; i++)
  83. {
  84. for (int j = 1; j <= s.size() - i; j++)
  85. {
  86. string t = s.substr(i, j);
  87. if (is_loop(t) && !is_any_return(t))
  88. {
  89. res = true;
  90. break;
  91. }
  92. }
  93. }
  94.  
  95. cout << (res ? "TAK" : "NIE") << endl;
  96. }
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0s 15240KB
stdin
4
WSEN
SEEES
EEEENWWSS
NNEESSNNWWSS
stdout
TAK
NIE
TAK
NIE