#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) {
setIn(s+".in");
setOut(s+".out");
} // for USACO
}
}
using namespace io;
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg> void ps(const Arg& first) {
pr(first); ps(); // no space at end of line
}
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
using namespace input;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
string s1, s2;
vector<ll> p1, p2;
void addone(vector<ll> &p) {
p[p.size()-1]++;
R0F (i, p.size()) {
if (p[i] == 2) {
p[i] = 0;
if (i == 0) {
p.insert(p.begin(), 1);
break;
} else {
p[i-1]++;
}
}
}
}
void subone(vector<ll> &p) {
p[p.size()-1]--;
R0F (i, p.size()) {
if (p[i] == -1) {
p[i] = 1;
if (i == 0) {
p.erase(p.begin());
break;
} else {
p[i-1]--;
}
}
}
}
int main() {
setIO();
re(s1, s2);
p1.push_back(1);
p2.push_back(1);
F0R (i, s1.length()) {
if (s1[i] == '1') {
p1.push_back(0);
}
if (s1[i] == '2') {
p1.push_back(1);
}
if (s1[i] == 'L') {
subone(p1);
}
if (s1[i] == 'R') {
addone(p1);
}
if (s1[i] == 'U') {
p1.pop_back();
}
}
F0R (i, s2.length()) {
if (s2[i] == '1') {
p2.push_back(0);
}
if (s2[i] == '2') {
p2.push_back(1);
}
if (s2[i] == 'L') {
subone(p2);
}
if (s2[i] == 'R') {
addone(p2);
}
if (s2[i] == 'U') {
p2.pop_back();
}
}
if (p1.size() > p2.size()) {
swap(p1, p2);
}
ll moves = 0;
while (p2.size()>p1.size()) {
moves++;
p2.pop_back();
}
ll ans = 1e13;
F0R (i, p1.size()) {
if (p1[i]<p2[i]) {
swap(p1, p2);
break;
} else if (p1[i] > p2[i]) {
break;
}
}
do {
bool toofar = false;
F0R (i, p1.size()) {
if (p1.size()-1 <= 12) {
break;
}
if (p1[i] != p2[i]) {
toofar = true;
}
}
if (!toofar) {
ll cnt = 0;
ll n1 = 0;
ll n2 = 0;
ROF (i, max((ll)p1.size()-12ll, 0ll), p1.size()) {
n1+=p1[i]*(1 << cnt);
n2+=p2[i]*(1 << cnt);
cnt++;
}
ans = min(ans, moves+n1-n2);
}
moves+=2;
p1.pop_back();
p2.pop_back();
} while (p1.size() >= 1);
ps(ans);
return 0;
}
Compilation message
board.cpp: In function 'int main()':
board.cpp:53:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
board.cpp:54:18: note: in expansion of macro 'FOR'
54 | #define F0R(i,a) FOR(i,0,a)
| ^~~
board.cpp:208:5: note: in expansion of macro 'F0R'
208 | F0R (i, s1.length()) {
| ^~~
board.cpp:53:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
board.cpp:54:18: note: in expansion of macro 'FOR'
54 | #define F0R(i,a) FOR(i,0,a)
| ^~~
board.cpp:225:5: note: in expansion of macro 'F0R'
225 | F0R (i, s2.length()) {
| ^~~
board.cpp:53:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
board.cpp:54:18: note: in expansion of macro 'FOR'
54 | #define F0R(i,a) FOR(i,0,a)
| ^~~
board.cpp:251:5: note: in expansion of macro 'F0R'
251 | F0R (i, p1.size()) {
| ^~~
board.cpp:53:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
board.cpp:54:18: note: in expansion of macro 'FOR'
54 | #define F0R(i,a) FOR(i,0,a)
| ^~~
board.cpp:261:9: note: in expansion of macro 'F0R'
261 | F0R (i, p1.size()) {
| ^~~
board.cpp: In function 'void io::setIn(std::string)':
board.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
board.cpp: In function 'void io::setOut(std::string)':
board.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
68 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
8 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
384 KB |
Output is correct |
2 |
Correct |
54 ms |
768 KB |
Output is correct |
3 |
Correct |
34 ms |
768 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
1424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
1268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
1236 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |