CF1117C.Magic Ship

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You a captain of a ship. Initially you are standing in a point (x1,y1)(x_1, y_1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x_2, y_2) .

You know the weather forecast — the string ss of length nn , consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s_1 , the second day — s2s_2 , the nn -th day — sns_n and (n+1)(n+1) -th day — s1s_1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (x,y)(x, y) to (x,y+1)(x, y + 1) ;
  • if wind blows the direction D, then the ship moves from (x,y)(x, y) to (x,y1)(x, y - 1) ;
  • if wind blows the direction L, then the ship moves from (x,y)(x, y) to (x1,y)(x - 1, y) ;
  • if wind blows the direction R, then the ship moves from (x,y)(x, y) to (x+1,y)(x + 1, y) .

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point (x,y)(x, y) it will move to the point (x1,y+1)(x - 1, y + 1) , and if it goes the direction U, then it will move to the point (x,y+2)(x, y + 2) .

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x_2, y_2) .

输入格式

The first line contains two integers x1,y1x_1, y_1 ( 0x1,y11090 \le x_1, y_1 \le 10^9 ) — the initial coordinates of the ship.

The second line contains two integers x2,y2x_2, y_2 ( 0x2,y21090 \le x_2, y_2 \le 10^9 ) — the coordinates of the destination point.

It is guaranteed that the initial coordinates and destination point coordinates are different.

The third line contains a single integer nn ( 1n1051 \le n \le 10^5 ) — the length of the string ss .

The fourth line contains the string ss itself, consisting only of letters U, D, L and R.

输出格式

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x_2, y_2) .

If it's impossible then print "-1".

输入输出样例

  • 输入#1

    0 0
    4 6
    3
    UUU
    

    输出#1

    5
    
  • 输入#2

    0 3
    0 0
    3
    UDD
    

    输出#2

    3
    
  • 输入#3

    0 0
    0 1
    1
    L
    

    输出#3

    -1
    

说明/提示

In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: (0,0)(0, 0) \rightarrow (1,1)(1, 1) \rightarrow (2,2)(2, 2) \rightarrow (3,3)(3, 3) \rightarrow (4,4)(4, 4) \rightarrow (4,6)(4, 6) .

In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: (0,3)(0, 3) \rightarrow (0,3)(0, 3) \rightarrow (0,1)(0, 1) \rightarrow (0,0)(0, 0) .

In the third example the ship can never reach the point (0,1)(0, 1) .

首页