Binary_string , (https://www.codechef.com/BEST2021/problems/BINARY00)

Practice

Author: Anurag dubey
Tester: Anurag dubey
Editorialist: Anurag dubey

DIFFICULTY:

CAKEWALK, SIMPLE, MEDIUM.

PREREQUISITES:

Math .

PROBLEM:

in given string we have to find output with help of these operation XOR, OR, AND and print output.

QUICK EXPLANATION:

just make a variable and do the given operation and store the answer in variable and in last print the answer.
we will take 1st char and 3rd char then we will perform given operation that is given in 2nd char and store the ans and do the same until last char comes.

EXPLANATION:

suppose given string is 1A1B1C1
first 1 AND with 1 output will 1 and it will stored then stored output will OR with 1 and again output will 1 and it will stored and stored output will XOR with 1 and output will 0 and we will store it and print it .

SOLUTIONS:

Setter's Solution
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. char Xor(char s1,char s2){

  4. if(s1==s2){

  5. return ‘0’;

  6. }

  • return ‘1’;
  1. }
  • char Or(char s1,char s2){
  1. if(s1==‘0’ && s2==‘0’){

  2. return ‘0’;

  3. }

  • return ‘1’;
  1. }
  • char And(char s1,char s2){
  1. if(s1==‘1’ && s2==‘1’){

  2. return ‘1’;

  3. }

  4. return ‘0’;

  • }

  • void testcase()

  1. {

  2. string ch;

  3. cin>>ch;

  4. char ans=ch[0];

  5. int k=ch.length();

  6. char ans1,ans2,alpha;

  7. for(int i=1;i<k;i+=2){

  8. alpha=ch[i];

  9. ans1=ch[i+1];

  10. if(alpha==‘A’){

  11. ans=And(ans,ans1);

  12. }

  13. else if(alpha==‘B’){

  14. ans=Or(ans,ans1);

  15. }

  16. else if(alpha==‘C’){

  17. ans=Xor(ans,ans1);

  18. }

  • }
  1. cout<<ans;

  2. return;

  3. }

  • int main()
  1. {

  2. int t;

  3. cin>>t;

  4. while(t–)

  5. {

  6. testcase();

  7. cout<<endl;

  8. }

  9. return 0;

  10. }

Tester's Solution
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. char Xor(char s1,char s2){

  4. if(s1==s2){

  5. return ‘0’;

  6. }

  • return ‘1’;
  1. }
  • char Or(char s1,char s2){
  1. if(s1==‘0’ && s2==‘0’){

  2. return ‘0’;

  3. }

  • return ‘1’;
  1. }
  • char And(char s1,char s2){
  1. if(s1==‘1’ && s2==‘1’){

  2. return ‘1’;

  3. }

  4. return ‘0’;

  • }

  • void testcase()

  1. {

  2. string ch;

  3. cin>>ch;

  4. char ans=ch[0];

  5. int k=ch.length();

  6. char ans1,ans2,alpha;

  7. for(int i=1;i<k;i+=2){

  8. alpha=ch[i];

  9. ans1=ch[i+1];

  10. if(alpha==‘A’){

  11. ans=And(ans,ans1);

  12. }

  13. else if(alpha==‘B’){

  14. ans=Or(ans,ans1);

  15. }

  16. else if(alpha==‘C’){

  17. ans=Xor(ans,ans1);

  18. }

  • }
  1. cout<<ans;

  2. return;

  3. }

  • int main()
  1. {

  2. int t;

  3. cin>>t;

  4. while(t–)

  5. {

  6. testcase();

  7. cout<<endl;

  8. }

  9. return 0;

  10. }

Editorialist's Solution
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. char Xor(char s1,char s2){

  4. if(s1==s2){

  5. return ‘0’;

  6. }

  • return ‘1’;
  1. }
  • char Or(char s1,char s2){
  1. if(s1==‘0’ && s2==‘0’){

  2. return ‘0’;

  3. }

  • return ‘1’;
  1. }
  • char And(char s1,char s2){
  1. if(s1==‘1’ && s2==‘1’){

  2. return ‘1’;

  3. }

  4. return ‘0’;

  • }

  • void testcase()

  1. {

  2. string ch;

  3. cin>>ch;

  4. char ans=ch[0];

  5. int k=ch.length();

  6. char ans1,ans2,alpha;

  7. for(int i=1;i<k;i+=2){

  8. alpha=ch[i];

  9. ans1=ch[i+1];

  10. if(alpha==‘A’){

  11. ans=And(ans,ans1);

  12. }

  13. else if(alpha==‘B’){

  14. ans=Or(ans,ans1);

  15. }

  16. else if(alpha==‘C’){

  17. ans=Xor(ans,ans1);

  18. }

  • }
  1. cout<<ans;

  2. return;

  3. }

  • int main()
  1. {

  2. int t;

  3. cin>>t;

  4. while(t–)

  5. {

  6. testcase();

  7. cout<<endl;

  8. }

  9. return 0;

  10. }