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;
const int N = 1e6 + 5;
int n, u[N], sz;
string a, b, s;
queue <int> q;
unordered_map <string, int> asd;
inline int f (string S){
return asd[S];
}
inline string F (int v){
string cur;
for (int i = 0; i < n; i++){
if ( v & (1 << i) )
cur = '1' + cur;
else
cur = '0' + cur;
}
return cur;
}
void dfs (int pos){
if (pos == n) {
int cur = 0, j = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == '1')
cur += (1 << j);
j++;
}
asd[s] = cur;
return;
}
s += '0';
dfs(pos + 1);
s.pop_back();
s += '1';
dfs(pos + 1);
s.pop_back();
}
int main(){
cin >> n;
cin >> a >> b;
dfs(0);
u[ f(a) ] = 1;
q.push( f(a) );
while (!q.empty()){
int v = q.front();
q.pop();
a = F(v);
for (int i = 0; i < n; i++){
s = a;
for (int j = i; j < n; j++){
a[j] = '1';
if ( u[ f(a) ] == 0 ){
u[ f(a) ] = u[v] + 1;
q.push( f(a) );
}
}
a = s;
}
for (int i = 0; i < n; i++){
s = a;
for (int j = i; j < n; j++){
a[j] = '0';
if ( u[ f(a) ] == 0 ){
u[ f(a) ] = u[v] + 1;
q.push( f(a) );
}
}
a = s;
}
for (int i = 0; i < n; i++){
s = a;
for (int j = i; j < n; j++){
a[j] = (a[j] == '0' ? '1' : '0');
if ( u[ f(a) ] == 0 ){
u[ f(a) ] = u[v] + 1;
q.push( f(a) );
}
}
a = s;
}
}
cout << u[ f(b) ] - 1 << endl;
}
# | 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... |