#PS0000142. Rút ngắn hành trình robot (Yet Another Walking Robot)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Rút ngắn hành trình robot (Yet Another Walking Robot)
Yet Another Walking Robot
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A robot starts at on the coordinate plane. Its route is a string of length , indexed from 1 to . Each character is a unit move: L left, R right, U up, or D down.
Delete exactly one nonempty contiguous interval , then execute the remaining moves in their original order. The final position must equal the original final position. Find a shortest interval to delete. Deleting the entire string is allowed, leaving the robot stationary. Any shortest valid interval is acceptable.
Input
- Line 1: integer , the route length.
- Line 2: string of moves.
Output
Print -1 if no deletion is possible. Otherwise print two integers separated by a space, with , the endpoints of a shortest interval to delete.
Subtasks
- Subtask 1 (20 points): ; ; Every character of belongs to the set
L,R,U,D. - Subtask 2 (30 points): ; ; Every character of belongs to the set
L,R. - Subtask 3 (50 points): ; ; Every character of belongs to the set
L,R,U,D.
Example 1
Input
4
LRUD
Output
1 2
Explanation
Deleting LR (moves 1 and 2) preserves the endpoint; UD also works. A single move changes the position, so length 1 cannot work.
Example 2
Input
4
LURD
Output
1 4
Explanation
The four moves form a closed loop. No adjacent moves cancel, and three moves cannot return because each move changes the parity of . All four moves must be deleted.
Example 3
Input
5
RRUDU
Output
3 4
Explanation
Moves 3 and 4 are UD and cancel. Length 2 is minimal because a single move cannot cancel.
Example 4
Input
5
LLDDR
Output
-1
Explanation
The visited positions, including the start, are . They are distinct, so no nonempty deletion preserves the endpoint.