pranay_teja
10/9/2018 - 7:59 AM

Citrix Shift and XOR

#include <bits/stdc++.h>
using namespace std;

// Citrix Shift and XOR
// #Strings #BitManipulation #Placement
/*
    Given encoded msg find the originalMsg which on k right shifts and XOR's gives encoded msg
    input :
    1
    110000
    2
    Output:
    10000
    
    Explaination:
    10000
     10000  // shifted to right and XOR'ed; all gaps are assumed to be 0
XOR:110000

*/

char XOR(char a, char b);
char opp(char c);
string originalMsg(string x, int k);

int main(){
    int t;
    cin>>t;
    while(t--){
        string x;
        cin>>x; // Encoded msg
        int k;
        cin>>k; // Number of times original msg is XOR'ed
        cout<<originalMsg(x,k)<<endl;
    }
    return 0;
}

string originalMsg(string x, int k){
    int n=x.size();
    string ans;
    ans.push_back(x[0]);
    for(int i=1;i<(n-(k-1));i++){
        char res=x[i];
        for(int j=1;j<k && i-j>=0 ;j++){
            res=XOR(res,ans[i-j]);
        }
        ans.push_back(res);
    }
    return ans;
}

char XOR(char a, char b){
    if(a==b){
        return '0';
    }else{
        return '1';
    }
}