1750C - Complementary XOR - CodeForces Solution


constructive algorithms data structures

Please click on ads to support us..

Python Code:

test=int(input())
while test:
    n=int(input())
    s1=input()
    s2=input()
    all_equal=0
    for i,j in zip(s1,s2):
        if(i==j):
            all_equal+=1
    if(all_equal==0 or all_equal==n):
        print("YES")
        ones=s1.count('1')
        if((all_equal==n and ones%2!=0) or (all_equal==0 and ones%2==0)):
            print(ones+3)
        else:
            print(ones)
        for i in range(n):
            if(s1[i]=='1'):
                print(i+1,i+1)
        if((all_equal==n and ones%2!=0) or (all_equal==0 and ones%2==0)):
            print(1,1)
            print(2,n)
            print(1,n)
    else:
        print("NO")
    test-=1

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define int long long


void solve(){
    int n; cin>>n;
    string a, b; cin>>a>>b;

    int diff = abs(a[0] - b[0]);
    for(int i = 0; i < n; i++){
        if(abs(a[i] - b[i]) != diff){
            cout<<"NO"<<endl;
            return;
        }
    }

    cout<<"YES"<<endl;

    int aones = 0, azeros = 0, bzeros = 0, bones = 0;
    for(auto &x : a){
        if(x == '1') aones++;
        else azeros++;
    }

    for(auto &x : b){
        if(x == '1') bones++;
        else bzeros++;
    }

    if(bones == n){
        if(aones == n){
            cout<<"2"<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
            return;
        }
        else{
            cout<<"3"<<endl;
            cout<<1<<" "<<n<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
            return;
        }
    }

    if(bones == 0){
        if(aones == 0){
            cout<<"0"<<endl;
            return;
        }
        else{
            cout<<"1"<<endl;
            cout<<1<<" "<<n<<endl;
            return;
        }
    }


    vector<int> zeros;
    for(int i = 0; i < n; i++) if(a[i] == '0') zeros.push_back(i + 1);

    azeros = azeros - ((a[0] == '0') ? 1 : 0);
    // cout<<azeros<<" ->>>> "<<endl;
    if(azeros % 2 == 0){
        // all b toggles acc to b[0].
        if(b[0] == '0'){
            //all b becomes 0.
            cout<<zeros.size() + 1<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<n<<endl;
        }
        else{
            //all b becomes 1.
            cout<<zeros.size() + 2<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
        }
    }
    else{
        if(b[0] == '0'){
            //all b becomes 1.
            cout<<zeros.size() + 2<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
        }
        else{
            //all b becomes 0.
            cout<<zeros.size() + 1<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<n<<endl;
        }
    }
}



int32_t main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    int t = 1;
    cin>>t;
    while(t--){
        solve();
    }
return 0;
}


Comments

Submit
0 Comments
More Questions

528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable