смотри, необходимо предподсчетом для каждой позиции посчитать сколько разных координат…
- User
смотри, необходимо предподсчетом для каждой позиции посчитать сколько разных координат есть, идем справа налево и считаем просто. пусть есть функция f(S,D), где s- номер, а d -направление #include #include #include
using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string commands; cin >> N >> commands; set possible_positions; int original_position = 0; int direction = 1; for (char command : commands) { if (command == 'F') { original_position += direction; } else if (command == 'R') { direction = 1; } else if (command == 'L') { direction = -1; } }
for (int i = 0; i < N; ++i) { int position = 0; direction = 1; for (int j = 0; j < i; ++j) { if (commands[j] == 'F') { position += direction; } else if (commands[j] == 'R') { direction = 1; } else if (commands[j] == 'L') { direction = -1; } } for (char new_command : {'F', 'R', 'L'}) { if (commands[i] == new_command) continue; int temp_position = position; int temp_direction = direction; if (new_command == 'F') { temp_position += temp_direction; } else if (new_command == 'R') { temp_direction = 1; } else if (new_command == 'L') { temp_direction = -1; } for (int j = i + 1; j < N; ++j) { if (commands[j] == 'F') { temp_position += temp_direction; } else if (commands[j] == 'R') { temp_direction = 1; } else if (commands[j] == 'L') { temp_direction = -1; } } possible_positions.insert(temp_position); } } cout << possible_positions.size() << "\n"; return 0; }
- Assistant