1704A - Two 0-1 Sequences - CodeForces Solution


constructive algorithms greedy

Please click on ads to support us..

Python Code:

def main():
    t = 1
    t = int(input())
    for _ in range(t):
        a, b = map(int, input().split())
        f = input()
        s = input()
        while f != s:
            if (len(f) <= 1):
                break
            if s[0] == '1':
                f = str(max(int(f[0]), int(f[1]))) + f[2:]
            else:
                f = str(min(int(f[0]), int(f[1]))) + f[2:]
            if len(f) < len(s):
                break
        if (f == s):
            print("YES")
        else:
            print("NO")


main()

C++ Code:

#include<bits/stdc++.h>
#define For(i,k,n)                    for (int i = k; i < n; i++)
#define RFor(i,n, k)                  for (int i = n; i >= k; i--)
#define pb                            push_back 
#define sz                            size()
#define all(vec)                      vec.begin(), vec.end()
#define int long long int
#define maxn 100000             //declared for ncrmod function
using namespace std;

//Finding NCR mod p ****************************************************************
int fac[maxn],invfact[maxn];
int powerforncr(int x,int y,int mod){
    int res=1;
    x=x%mod;
    while (y > 0){
        if (y & 1)
            res = (res%mod * x%mod) % mod;

        y = y >> 1; 
        x = (x%mod * x%mod) % mod;
    }
    return res;
}

int modInverse(int n,int mod){
    return powerforncr(n,mod-2,mod);
}

void ncrmodpPrecompute(int mod){
    fac[0]=1;
    invfact[0]=1;
    for(int i=1;i<maxn;i++){
        fac[i]=(fac[i-1]%mod * i%mod)%mod;
        invfact[i]=modInverse(fac[i],mod);
    }
}

int ncrmod(int n,int r,int mod){
    if(r<0 or n<0){
        assert(false);
    }
    
    if(n<r)
        return 0;
        
    if(r==0 or r==n)
        return 1;
        
    return (fac[n] * invfact[r] % mod) * invfact[n-r] % mod;
}

//Finding ncr ******************************************************************************
int nCr(int n,int r) {
    int p = 1;
    int k = 1;
    if (n-r<r)
        r=n-r;
    
    if(r!=0) {
        while(r) {
            k=k*r;
            p=p*n;
            int m=__gcd(p,k);
            p/=m;
            k/=m;
            r--;
            n--;
        }
    }
    else {
        p=1;
    }
    return p;
}

//Checking prime number***********************************************************
bool isPrime(int n) {
    if(n==1)
        return false;
    if (n==2 or n==3)
        return true;
    if (n%2==0 or n%3==0)
        return false;
    for(int i=5;i*i<=n;i+=6) {
        if(n%i==0 or n%(i+2)==0)
            return false;
   }
   return true;
}

//Finding sum of digits *************************************************************
int sumofdigits(int n) {
    int ans=0;
    while (n>0) {
        ans=(ans+ n%10);
        n/=10;
    }
    return ans;
}

//Finding a^b *************************************************************************
int power(int a,int b) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a * a);
        b = b >> 1;
    }
    return res;
}
 
int powermod(int a, int b, int mod) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
        b = b >> 1;
    }
    return res;
}
 
//Finding MEX ****************************************************************************
int findMEX(vector<int>v) {  //MEX of {1,2,3,4} is 0
    int n=v.size();
    map<int,int>mp;
    for(int i=0;i<n;i++) {
        mp[v[i]]++;
    }
    for(int i=0;i<=n+1;i++) {
        if(mp[i]==0){
        return i;
        }
    }
    return n+1;
}

//Finds all the prime numbers from 1 to n [1,n] (inclusive)
vector<int> SieveOfEratosthenes(int n){
    vector<bool>prime(n+1,true);
    for (int p=2;p*p<=n;p++) {
        if (prime[p]) {
            for (int i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
    
    vector<int>v;
    for(int i=2;i<=n;i++)
        if(prime[i])
            v.pb(i);

    return v;
}
    
//Finding unique prime factors  eg : For n = 30 , it returns { 2 , 3 , 5 }
vector<int> findUniquePrimeFactors(int n){
    vector<int>v;
    for(int j=2;j*j<=n;j++){
        if(n%j==0){
            v.pb(j);
            while(n%j==0){
                n/=j;
            }
        }
    }
    if(n>1)
        v.pb(n);
    return v;
}
//***************************************************Begin********************************************************

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

    int i=n-1,j=m-1;
    while(j>0){
        if(a[i]!=b[j]){
            cout<<"NO"<<endl;
            return;
        }
        i--;
        j--;
    }
    // cout<<i<<" "<<j<<endl;
    for(int k=i;k>=0;k--){
        if(a[k]==b[j]){
            cout<<"YES"<<endl;
            return;
        }
    }
    cout<<"NO"<<endl;
}

void init_code(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
}
int32_t main(){
    init_code();
    //define precomputations below
    //precomputation definations ended
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
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