#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
void print(vector<T> v)
{
cout << " ---> ";
for (auto &x : v)
{
cout << x << " ";
}
cout << "\n";
}
vector<int> solve(string s)
{
vector<int> cur;
for (auto &ch : s)
{
if (ch == '1')
{
cur.push_back(0);
continue;
}
if (ch == '2')
{
cur.push_back(1);
continue;
}
if (ch == 'U')
{
cur.pop_back();
continue;
}
if (ch == 'R')
{
int p = (int) cur.size() - 1;
while (1)
{
cur[p] ^= 1;
if (cur[p] == 1)
{
break;
}
p--;
assert(p >= 0);
}
continue;
}
if (ch == 'L')
{
int p = (int) cur.size() - 1;
while (1)
{
cur[p] ^= 1;
if (cur[p] == 0)
{
break;
}
p--;
assert(p >= 0);
}
continue;
}
assert(0);
}
return cur;
}
int get_level(ll x)
{
if (x == 1)
{
return 1;
}
else
{
assert(x >= 2);
return 1 + get_level(x / 2);
}
}
ll solve(ll a, ll b)
{
if (a == b)
{
return 0;
}
if (get_level(b) > get_level(a))
{
return 1 + solve(a, b / 2);
}
if (get_level(a) > get_level(b))
{
return 1 + solve(a / 2, b);
}
assert(get_level(a) == get_level(b));
return min(abs(a - b), 2 + solve(a / 2, b / 2));
}
int main()
{
#ifdef ONPC
freopen ("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
string s, t;
cin >> s >> t;
vector<int> x = solve(s), y = solve(t);
ll a, b;
a = 1;
b = 1;
for (int i = 0; i < (int) x.size(); i++)
{
a = a * 2 + x[i];
}
for (int i = 0; i < (int) y.size(); i++)
{
b = b * 2 + y[i];
}
cout << solve(a, b) << "\n";
return 0;
}
/**
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1080 ms |
1228 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
1172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1084 ms |
1156 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |