#include <bits/stdc++.h>
using namespace std;
const int N = 1<<17;
string add(string a, string b) {
string c,d;
int p=0,t,i;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
while(a.size()<b.size()) a.push_back('0');
while(a.size()>b.size()) b.push_back('0');
for(i=0;i<(int)(a.size());i++) {
t=p+a[i]-'0'+b[i]-'0';
p=t/10;
t%=10;
c.push_back(t+'0');
}
if(p>0) c.push_back(p+'0');
reverse(c.begin(),c.end());
for(i=0;i<(int)(c.size());i++) if(c[i]!='0') break;
for(;i<(int)(c.size());i++) d.push_back(c[i]);
if(d.empty()) d="0";
return d;
}
bool cmp(string a, string b) {
if(a.size()<b.size()) return true;
if(a.size()>b.size()) return false;
for(int i=0;i<(int)(a.size());i++) {
if(a[i]<b[i]) return true;
if(a[i]>b[i]) return false;
}
return false;
}
string get_min(string a, string b) {
return ((cmp(a,b)==true) ? a : b);
}
string get_max(string a, string b) {
return ((cmp(a,b)==true) ? b : a);
}
string subtract(string a, string b) {
string c,d;
int i,j;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
while(a.size()>b.size()) b.push_back('0');
for(i=0;i<(int)(a.size());i++) {
if(a[i]>=b[i]) {
c.push_back(a[i]-b[i]+'0');
continue;
}
for(j=i+1;j<(int)(a.size());j++) {
if(a[j]>'0') {
--a[j];
break;
}
else {
a[j]='9';
}
}
c.push_back(a[i]+10-b[i]+'0');
}
reverse(c.begin(),c.end());
for(i=0;i<(int)(c.size());i++) if(c[i]!='0') break;
for(;i<(int)(c.size());i++) d.push_back(c[i]);
if(d.empty()) d="0";
return d;
}
string div2(string a) {
int i,p=0;
string b,c;
for(i=0;i<(int)(a.size());i++) {
p*=10;
p+=a[i]-'0';
b.push_back(p/2+'0');
p%=2;
}
for(i=0;i<(int)(b.size());i++) if(b[i]!='0') break;
for(;i<(int)(b.size());i++) c.push_back(b[i]);
if(c.empty()) c="0";
return c;
}
struct node {
int level;
string number;
node() {
level=0;
number="1";
}
void move(char a) {
if(a=='1') {
++level;
number=add(number,number);
}
else if(a=='2') {
++level;
number=add(add(number,number),"1");
}
else if(a=='L') {
number=subtract(number,"1");
}
else if(a=='R') {
number=add(number,"1");
}
else { //a=='U'
--level;
number=div2(number);
}
}
};
char a[N],b[N];
int n,m;
node node1,node2;
string ans,curr;
int main() {
int i;
scanf("%s", a+1);
n=strlen(a+1);
scanf("%s", b+1);
m=strlen(b+1);
for(i=1;i<=n;i++) {
node1.move(a[i]);
}
for(i=1;i<=m;i++) {
node2.move(b[i]);
}
//ans=node1.level+node2.level;
//if(node1.level>=1 && node2.level>=1) ans=min(ans,node1.level+node2.level-1ll);
curr="0";
while(node1.level!=node2.level) {
if(node1.level>node2.level) curr=add(curr,"1"),node1.move('U');
else curr=add(curr,"1"),node2.move('U');
}
ans=add(curr,subtract(get_max(node1.number,node2.number),get_min(node1.number,node2.number)));
while(node1.level>0) {
curr=add(curr,"2");
node1.move('U');
node2.move('U');
ans=get_min(ans,add(curr,subtract(get_max(node1.number,node2.number),get_min(node1.number,node2.number))));
}
printf("%s\n", ans.c_str());
return 0;
}
Compilation message
board.cpp: In function 'int main()':
board.cpp:140:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", a+1);
^
board.cpp:142:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", b+1);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2284 KB |
Output is correct |
2 |
Correct |
0 ms |
2284 KB |
Output is correct |
3 |
Correct |
0 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2284 KB |
Output is correct |
2 |
Correct |
3 ms |
2284 KB |
Output is correct |
3 |
Correct |
9 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2284 KB |
Output is correct |
2 |
Correct |
0 ms |
2284 KB |
Output is correct |
3 |
Correct |
0 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2284 KB |
Output is correct |
2 |
Correct |
63 ms |
2284 KB |
Output is correct |
3 |
Correct |
29 ms |
2284 KB |
Output is correct |
4 |
Correct |
0 ms |
2284 KB |
Output is correct |
5 |
Correct |
0 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2284 KB |
Output is correct |
2 |
Correct |
0 ms |
2284 KB |
Output is correct |
3 |
Correct |
0 ms |
2284 KB |
Output is correct |
4 |
Correct |
0 ms |
2284 KB |
Output is correct |
5 |
Correct |
0 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2284 KB |
Output is correct |
2 |
Correct |
69 ms |
2284 KB |
Output is correct |
3 |
Correct |
0 ms |
2284 KB |
Output is correct |
4 |
Correct |
0 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
200 ms |
2284 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
200 ms |
2416 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
200 ms |
2416 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
200 ms |
2416 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |