(AtCoder Beginner Contest 291: C - LRUD Instructions 2)

143 阅读1分钟

思维

#include <bits/stdc++.h>

using i64 = long long;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n;
	std::cin >> n;

	std::string s;
	std::cin >> s;

	int x = 0, y = 0;
	std::set<std::pair<int, int>> visited;
	visited.emplace(x, y);

	for (auto c : s) {
		if (c == 'L') {
			x--;
		} else if (c == 'R') {
			x++;
		} else if (c == 'U') {
			y++;
		} else {
			y--;
		}
		if (visited.count({x, y})) {
			std::cout << "Yes\n";
			return 0;
		}
		visited.emplace(x, y);
	}
	std::cout << "No\n";

	return 0;
}