This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 first_dif = -1;
int get(vector<int>& x, vector<int>& y)
{
if ((int) x.size() > (int) y.size())
{
x.pop_back();
return 1 + get(x, y);
}
if ((int) x.size() < (int) y.size())
{
y.pop_back();
return 1 + get(x, y);
}
if (x == y)
{
return 0;
}
assert((int) x.size() == (int) y.size());
assert((int) x.size() >= 1);
assert((int) y.size() >= 1);
int lst_x = x.back(), lst_y = y.back();
x.pop_back();
y.pop_back();
int sol = 2 + get(x, y);
x.push_back(lst_x);
y.push_back(lst_y);
/// dif = what ?
assert(first_dif != -1);
int inside = (int) x.size() - first_dif;
if (inside <= 7)
{
int a = 1, b = 1;
for (int i = first_dif; i < (int) x.size(); i++)
{
a = 2 * a + x[i];
}
for (int i = first_dif; i < (int) y.size(); i++)
{
b = 2 * b + y[i];
}
sol = min(sol, abs(a - b));
}
return sol;
}
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);
for (int i = 0; i < (int) x.size() && i < (int) y.size(); i++)
{
if (x[i] != y[i])
{
first_dif = i;
break;
}
}
cout << get(x, y) << "\n";
return 0;
}
/**
**/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |