import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
class board
{
static final BigInteger BIGTWO = BigInteger.valueOf(2);
static BigInteger min(BigInteger a, BigInteger b)
{
return a.compareTo(b) < 0 ? a : b;
}
static int Depth(String a)
{
int depth = 0;
for (int i = 0; i < a.length(); ++i)
if (a.charAt(i) == 'U')
--depth;
else if (a.charAt(i) == '1' || a.charAt(i) == '2')
++depth;
return depth;
}
static BigInteger Coord(String a)
{
BigInteger coord = BigInteger.ZERO;
for (int i = 0; i < a.length(); ++i)
switch(a.charAt(i))
{
case '1':
coord = coord.multiply(BIGTWO); break;
case '2':
coord = coord.multiply(BIGTWO);
coord = coord.add(BigInteger.ONE); break;
case 'L':
coord = coord.subtract(BigInteger.ONE); break;
case 'R':
coord = coord.add(BigInteger.ONE); break;
case 'U':
coord = coord.divide(BIGTWO);
}
return coord;
}
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
String path = in.nextString();
int aDepth = Depth(path);
BigInteger aCoord = Coord(path);
path = in.nextString();
int bDepth = Depth(path);
BigInteger bCoord = Coord(path);
if (aDepth > bDepth)
{
int temp1 = aDepth; aDepth = bDepth; bDepth = temp1;
BigInteger temp2 = aCoord; aCoord = bCoord; bCoord = temp2;
}
int depthSteps = bDepth - aDepth;
bDepth = aDepth;
bCoord = bCoord.shiftLeft(-depthSteps);
BigInteger ans = aCoord.subtract(bCoord).abs().add(BigInteger.valueOf(depthSteps));
while (aDepth-- > 0)
{
depthSteps += 2;
aCoord = aCoord.divide(BIGTWO);
bCoord = bCoord.divide(BIGTWO);
ans = min(ans, aCoord.subtract(bCoord).abs().add(BigInteger.valueOf(depthSteps)));
}
out.println(ans);
out.close();
}
static class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String nextString()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try { tokenizer = new StringTokenizer(reader.readLine()); }
catch (IOException e) { throw new RuntimeException(e); }
}
return tokenizer.nextToken();
}
public int nextInt()
{
return Integer.parseInt(nextString());
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
9920 KB |
Output is correct |
2 |
Correct |
80 ms |
10100 KB |
Output is correct |
3 |
Correct |
80 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
24960 KB |
Output is correct |
2 |
Correct |
118 ms |
13724 KB |
Output is correct |
3 |
Correct |
198 ms |
25000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
9764 KB |
Output is correct |
2 |
Correct |
80 ms |
9988 KB |
Output is correct |
3 |
Correct |
77 ms |
9972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
14744 KB |
Output is correct |
2 |
Execution timed out |
239 ms |
32288 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
9960 KB |
Output is correct |
2 |
Correct |
108 ms |
10204 KB |
Output is correct |
3 |
Correct |
84 ms |
10080 KB |
Output is correct |
4 |
Correct |
81 ms |
9856 KB |
Output is correct |
5 |
Correct |
80 ms |
9888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
13508 KB |
Output is correct |
2 |
Correct |
160 ms |
14560 KB |
Output is correct |
3 |
Correct |
86 ms |
10120 KB |
Output is correct |
4 |
Correct |
88 ms |
9912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
22500 KB |
Output is correct |
2 |
Execution timed out |
369 ms |
46556 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1111 ms |
190420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1043 ms |
153608 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1022 ms |
181908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |