fork download
  1. #include<stdio.h>
  2. #include<set>
  3. using namespace std;
  4. char S[1212];
  5. int dx[4] = { 1,-1,0,0 };
  6. int dy[4] = { 0,0,1,-1 };
  7. int T[512], R[4] = { 1,0,3,2 };
  8. set<pair<int, int>>M;
  9. int main() {
  10. T['N'] = 0, T['S'] = 1, T['W'] = 2, T['E'] = 3;
  11. int n,nowx,nowy, ans = 0;
  12. scanf("%d%s", &n,S);
  13. nowx = nowy = 0;
  14. M.insert({ 0,0 });
  15. bool flag = 0;
  16. for (int i = 0; i < n; i++) {
  17. for (int j = 0; j < 2; j++) {
  18. nowx += dx[T[S[i]]], nowy += dy[T[S[i]]];
  19. if (flag == 0 && M.find({ nowx,nowy }) != M.end() && T[S[i - 1]] != R[T[S[i]]]) ans++;
  20. flag = (M.find({ nowx,nowy }) != M.end());
  21. M.insert({ nowx,nowy });
  22. }
  23. }
  24. printf("%d", ans);
  25. return 0;
  26. }
Success #stdin #stdout 0s 4504KB
stdin
14
NNNESWWWSSEEEE
stdout
2