Problem E : Eric’s Work
时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 1048576K,其他语言2097152K
Special Judge, 64bit IO Format: %lld
题目描述
A binary string is a string consisting of only 0’s and 1’s. Elsa, Eric’s boss, gave him a binary string s of length 20 and asked him to modify s into another binary string t within D days.
Eric really hates this task and therefore never modifies more than one character in a day. However, being forced to show Elsa the daily progress, Eric must modify some characters of the string every day. That means, the only possible way for Eric is to modify exact one character in each day before he finishes the task.
It is obviously cheating to have a character changed to something other than 0 and 1. Moreover, Eric will be caught cheating if the string is modified into the same binary string twice since Elsa has a good memory. That is, before the string is modified into t, all modifications result in unique strings. Note that Eric cannot modify the string into s which is the string given by Elsa, either.
Eric wants to spend as much time as possible. He is wondering if he can spend exact D days to have the string s modified into t. Please write a program to help Eric.
输入描述:
The input contains three lines. The first line contains a binary string s. The second line contains a binary string t. The third line contains an integer D. Elsa asked Eric to modify the binary string s into t within D days.
输出描述:
If there is no way to achieve what Eric wants, output -1. Otherwise, output D lines to represent one possible way. The i-th line contains a binary string, the result of the modification on the i-th day.
示例1
输入
复制
00000000000000000000 11111111111111111111 5
输出
复制
-1
示例2
输入
复制
00000000001111111111 10000000001111111111 5
输出
复制
00000000001111111110 00000001001111111110 10000001001111111110 10000000001111111110 10000000001111111111
备注:
•The strings s and t consist of only 0’s and 1’s. •The length of s and the length of t are both 20. •1 ≤ D ≤ 500000 •If there are multiple solutions, then you may output any of them.
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
int a[N + 25], b[N + 25], tot;
char s1[25], s2[25], tmp[25];
int st, ed, dx, stp;
int low(int x) {return x & (-x); }
int cnt(int x) {
int ret = 0;
while (x) ret ++, x -= low(x);
return ret;
}
int dis(int x, int y) {
return cnt(x ^ y);
}
void num2str(int x) {
for (int i = 0; i < 20; i ++) {tmp[i] = '0' + (x % 2); x /= 2;}
tmp[20] = 0;
}
bool dfs(int now, int lm) {
if (lm == 0) {
if (cnt(now ^ dx) == 0) return true;
return false;
}
for (int i = 0; i < 20; i ++) {
int nm = 1 << i;
if (a[now ^ nm] == 1) continue ;
if (cnt(now ^ nm ^ dx) > lm - 1) continue ;
if (cnt(now ^ nm ^ dx) == 0 && lm - 1 > 0) continue ;
a[now ^ nm] = 1; b[++ tot] = now ^ nm;
bool res = dfs(now ^ nm, lm - 1);
if (res) return true;
a[now ^ nm] = 0; tot --;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s1; cin >> s2;
cin >> stp;
for (int i = 19; i >= 0; i --) {
st *= 2; ed *= 2;
st += s1[i] - '0';
ed += s2[i] - '0';
}
dx = st ^ ed;
int nw = 0;
int lm = stp;
if (stp < cnt(dx)) {
cout << -1 << endl;
} else if ((stp - cnt(dx)) % 2) {
cout << -1 << endl;
} else {
bool ck = dfs(0, lm);
if (ck) {
for (int i = 1; i <= stp; i ++) {
num2str(st ^ b[i]);
cout << tmp << endl;
}
} else cout << -1 << endl;
}
return 0;
}