1937B - Binary Path - CodeForces Solution


implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int  long long
#define test int t;cin>>t;while(t--)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl '\n'
using namespace std;

const int M=1e9+7;

int nCr(int n, int r) {
    int sum=1;
     for(int i = 1; i <= r; i++){
            sum = sum * (n - r + i) / i;
        }
    return sum;
}
//O(log(min(a,b)))
int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a % b);
}   
//O(log(min(a,b)))
int lcm(int a, int b){
    return (a / gcd(a, b)) * b;
}

//O(n)
int maxSubArraySum(int a[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;
 
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

int sqroot(int n){
    int l = 0, h = 1000000000,ans = n;
    while(l <= h){
        int mid = l + (h-l)/2;
        if( mid*mid <= n){
            ans = mid;
            l = mid + 1;
        }
        else
            h = mid - 1;
    }
    return ans;
}


bool isPowerOfTwo(unsigned int n){
return (n&&!(n&n-1));
}

int countSetBits(int n){
    int count=0;
    while(n){
        n=n&(n-1);
        count++;
    }
    return count;
}

void prime(int n){
    for(int i=0;i*i<n;i++){
        while(n%i == 0)
        {
            cout<<i<<" ";
            n=n/i;
        }
    }
    if(n>1) cout<<n<<" ";
}

bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i <= n / 2; i++)
        if (n % i == 0)
            return false;
  
    return true;
}

int power(int a,int b){
    if(b==0) return 1;
    int tmp=power(a,b/2);
    int result=((tmp%M)*(tmp%M))%M;
    if(b%2==1) result =((result%M)*(a%M))%M;
    return result;
}

//int div(int a,int b){
//return a*power(b,M-2)%M;
//}

int mul(int a,int b){
return ((a%M)*(b%M)%M);
}


//___________________________________________________________________//

//FOR DEBUGGING
#define printarr(arr) for(int i=0;i<(sizeof(arr)/sizeof(arr[0]));i++){ cout << arr[i] << " ";}cout << endl;
#define printvec(v) for(int i=0;i<v.size();i++){ cout << v[i] << " ";}cout << endl;
#define printset(s) for(auto i : s){cout << i << " ";}cout<<endl;

//____________________________________________________________________//


void solve(){
    test{
        int n;
        cin>>n;
        string s1,s2;
        cin>>s1>>s2;
        string ans;
        int last=n-1;
        int cnt=0;
        for(int i=0;i<n-1;i++){
            if(s1[i+1]>s2[i]){
                last=i;
                break;
            }
        }
        //cout<<last<<endl;
        for(int i=last-1;i>=0;i--){
            if(s1[i+1]==s2[i]){
                cnt++;
            }
            else
            break;
        }
        for(int i=0;i<=last;i++){
            cout<<s1[i];
        }
        for(int i=last;i<n;i++){
            cout<<s2[i];
        }
        cout<<endl;
        cout<<cnt+1<<endl;

    }
}

signed main(){

    //do-not-use this in case of interactive
    ios_base::sync_with_stdio(0); //
    cin.tie(0);                   //
    //   //   //  //  //  //  //  //  

    solve();

    //IF YOU ARE GETTING ERROR:

    //RUNTIME: 1.Check if all indices are within bounds
    //         2.Check if div by zero is not happening

    //TLE:     1.Check if infinite loop is formed at any test-case
    //         2.Calculate time complexity(10e8 oper === 1sec) 

    //WA:      1.See if you are missing any edge-case(like n=1)
    //         2.Re-read the question
   
}


Comments

Submit
1 Comments
  • 1/3/2024 13:31 - Asia/Calcutta

hi


More Questions

2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff