Help with Problem C - "binary string reconstruction" from codeforces edu. round 94

Problem link: Problem - C - Codeforces

please somebody help me with my code for the problem C-Binary String Reconstruction. the code is running fine on the test cases but shoes wrong ans;

Approach- I firstly created a string ans of same size as string s with each element to be ‘a’. Then I itereate over string s and if s[i]==‘0’ then I put ans[i+x] =‘0’ (if i+x is within length of s) and ans[i-x] = ‘0’ (if i-x is within length of s).Then I iterate over s once again ,if s[i]==‘1’ then if ans[i+x]=‘0’ (if i+x is within length of s) and a[i-x]=‘0’ then i set flag as 1; at last iterate over string ans and replace all the remaining ‘a’ with ‘1’; At last if flag=1, print -1 else print ans;

my solution link: Submission #91052214 - Codeforces

solution:
#include<bits/stdc++.h>
#define endl ‘\n’
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lli long long int
using namespace std;
int main(){
boost;
lli t;
cin>>t;
while(t- -)
{
lli flag=0;
string s;
cin>>s;
lli n;
cin>>n;
string ans(s.size(),‘a’);

   for(lli i=0;i<s.size();i++)
   {
       if (s[i]=='0')
       {
           if (i+n<=s.size()-1)
           {
               
               ans[i+n]='0';
           }
           if (i-n>=0)
           {
               
               ans[i-n]='0';
           }
       }
   }
   
   for (lli i=0;i<s.size();i++ )
   {
       if (s[i]=='1')
       {
           if (i-n>=0 && i+n<=s.size()-1 && ans[i-n]=='0' && ans[i+n]=='0')
           {
               flag=1;
               break;
           }
           
           else if (i-n<0 && i+n<=s.size()-1 && ans[i+n]=='0')
           {
               flag=1;
               break;
           }
           else if (i-n>=0 && i+n>s.size()-1 && ans[i-n]=='0' )
           {
               flag=1;
               break;
           }
           
       }
   }
   
   for (lli i=0;i<s.size();i++)
   {
       if (ans[i]=='a')
       {
           ans[i]='1';
       }
   }
   
   
   if (flag==1)
   {
       cout<<-1<<endl;
   }
   else 
   {
       cout<<ans<<endl;
   }
   
    
} 

}

please help me out.

there will be n substrring .So if you take 1st element then you have to take 3rd element .
so just print all element at odd position

#include<bits/stdc++.h> 
#define endl '\n'
#define boost  ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lli long long int
using namespace std;
int main(){
    boost;
    lli t;
    cin>>t;
    while(t--)
    {
       lli flag=0;
       string s;
       cin>>s;
       lli n;
       cin>>n;
       string ans(s.size(),'a');
       
       
       for(lli i=0;i<s.size();i++)
       {
           if (s[i]=='0')
           {
               if (i+n<=s.size()-1)
               {
                   
                   ans[i+n]='0';
               }
               if (i-n>=0)
               {
                   
                   ans[i-n]='0';
               }
           }
       }
       
       for (lli i=0;i<s.size();i++ )
       {
           if (s[i]=='1')
           {
               int c=0;
               
               if (i+n<=s.size()-1 && ans[i+n]=='a')
               {
                   c++;
                   
               }
               
               if (i-n>=0 && ans[i-n]=='a' )
               {
                   c++;
               }
               
               if(c==0)
               {
                   flag=1;
                   break;
               }
               
           }
       }
       
       for (lli i=0;i<s.size();i++)
       {
           if (ans[i]=='a')
           {
               ans[i]='1';
           }
       }
       
       
       if (flag==1)
       {
           cout<<-1<<endl;
       }
       else 
       {
           cout<<ans<<endl;
       }
       
        
    }
    
   
}

I did some minute changes in your code in for loop where you were rechecking if s[i]=1.
Here your approach was setting flag=1 if you didn’t find 1 in left and right side i did the same thing but in simple way hope u will understand my changes, in case you didn’t understood changes made by me you can ping me.

1 Like

Bro that was A question.

thanks a lot @maskmanlucifer . I now identified my mistake…thanks for all your help… :star_struck: