PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000142. Rút ngắn hành trình robot (Yet Another Walking Robot)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 (0,0)(0,0) on the coordinate plane. Its route is a string ss of length nn, indexed from 1 to nn. Each character is a unit move: L left, R right, U up, or D down.

Delete exactly one nonempty contiguous interval sl,…,srs_l,\ldots,s_r, 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 nn, the route length.
  • Line 2: string ss of nn moves.

Output

Print -1 if no deletion is possible. Otherwise print two integers l,rl,r separated by a space, with 1≤l≤r≤n1\le l\le r\le n, the endpoints of a shortest interval to delete.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; ∣s∣=n|s|=n; Every character of ss belongs to the set L, R, U, D.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; Every character of ss belongs to the set L, R.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; Every character of ss 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 x+yx+y. 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 (0,0),(−1,0),(−2,0),(−2,−1),(−2,−2),(−1,−2)(0,0),(-1,0),(-2,0),(-2,-1),(-2,-2),(-1,-2). They are distinct, so no nonempty deletion preserves the endpoint.