C. Binary String Reconstruction——思维题Day2

C. Binary String Reconstruction

题目传送门

题意:

给你结果,要你求源串。
转换方法:
C. Binary String Reconstruction——思维题Day2_第1张图片

思路:

先全部置为1。
很简单,转换后0,一定前后x个为0(若存在)。
转换后的1,必然前后x都不为0,若为0则为-1输出。

代码:

#include 
#include 
#include 
#include 
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;
int main() {
     
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
     
        string str;
        string s;
        int x;
        bool flag = 0;
        cin >> s;
        cin >> x;
        int len = s.size();
       // cout<
       // cout<
        for (int i = 0; i < len; i++) {
     
            str += "1";
        }
        for (int i = 0; i < len; ++i) {
     
            if (s[i] == '0' && i - x >= 0) str[i - x] = '0';
            if (s[i] == '0' && i + x < len) str[i + x] = '0';
        }
       // cout<
        for (int i = 0; i < len; ++i) {
     
            if (s[i] == '0')continue;
            bool flag1 = 0;
            if ((i - x >= 0 && str[i - x] == '1') || (i + x < len && str[i + x] == '1')) flag1 = 1;
            if (!flag1) {
     
                flag = 1;
                break;
            }
        }
        if (flag) cout << -1 << endl;
        else cout << str << endl;
    }
    return 0;
}

你可能感兴趣的:(C. Binary String Reconstruction——思维题Day2)